📄 utilities.cs
字号:
using System;
using System.Collections.Generic;
using System.Text;
using System.Web.UI;
using System.Web.UI.WebControls;
using ESRI.ArcGIS.ADF.Web.DataSources;
using ESRI.ArcGIS.ADF.Web.UI.WebControls;
using ESRI.ArcGIS.ADF.Web;
using ESRI.ArcGIS.ADF.Web.Geometry;
namespace SelectToolTask
{
public class Utilities
{
#region GetQueryLayers method
/// <summary>
/// Gets a dropdownlist of queryable layers in
/// the resources associated with a Map control.
/// If layerNames contains layer names, names
/// are verified and added to the dropdownlist.
/// If layerNames is null or empty, all queryable
/// vector layers are returned.
/// </summary>
/// <param name="mapControl">Map control with resources in a
/// buddied MapResourceManager.</param>
/// <param name="layerNames">Null to retrieve all queryable
/// layers, or a list of layers to validate.</param>
/// <param name="idDelimiter">Delimiter between resource and layer names</param>
/// <param name="layerItems">Output: ListItemCollection with available
/// query layers. Item text is layer name;
/// item value is map resource name + idDelimiter + layer ID.</param>
/// <returns>False if any resource/functionality in Map control
/// is null (i.e., map service is not responding or is down);
/// true otherwise.</returns>
public static ListItem[] GetQueryLayers(Map mapControl, string layerNames,
string idDelimiter, out string[] invalidServices)
{
List<ListItem> layerColl = new List<ListItem>();
invalidServices = null;
if (mapControl != null)
{
GISResourceCollection gisColl
= Utilities.GetGISResources(mapControl, out invalidServices);
foreach (IGISResource gisResource in gisColl)
{
List<ListItem> queryLayers =
Utilities.GetQueryableLayersInResource(
gisResource, idDelimiter);
foreach (ListItem item in queryLayers)
{
bool addLayer = false;
// if no selection layers specified, include all vector layers
if (String.IsNullOrEmpty(layerNames))
addLayer = true;
else
{
// check if the layer is in the layer list
string[] layerNameArray =
layerNames.Split(new string[] {"::"}, StringSplitOptions.None);
for (int j = 0; j < layerNameArray.Length; j++)
if (String.Compare(item.Text, layerNameArray[j],
true, System.Globalization.CultureInfo.InvariantCulture)
== 0)
{
addLayer = true;
break;
}
}
if (addLayer)
layerColl.Add(item);
}
}
}
return layerColl.ToArray();
}
#endregion
#region GetQueryableLayersInResource
internal static List<ListItem> GetQueryableLayersInResource(IGISResource resource, string idDelimiter)
{
List<ListItem> qLayers = new List<ListItem>();
// Make sure the resource supports querying (point/line/polygon layers included)
// -- also exclude graphics layers
if (resource != null
&& resource.SupportsFunctionality(typeof(IQueryFunctionality))
&& !(resource is ESRI.ArcGIS.ADF.Web.DataSources.Graphics.MapResource))
{
IQueryFunctionality queryFunc = (IQueryFunctionality)
resource.CreateFunctionality(typeof(IQueryFunctionality), null);
string[] layerIds;
string[] layerNames;
// Get point/line/polygon layers
queryFunc.GetQueryableLayers(null, out layerIds, out layerNames);
if (layerNames != null && layerNames.Length > 0 &&
layerNames.Length == layerIds.Length)
{
// Add each layer to list (include resource name in case of duplicate layer IDs)
for (int i = 0; i < layerNames.Length; i++)
{
// insert layer at top of list to preserve drawing order in the data source
qLayers.Add(new ListItem(layerNames[i],
String.Concat(resource.Name, idDelimiter, layerIds[i])));
}
qLayers.Reverse();
}
}
return qLayers;
}
#endregion
#region ToAdfGeometry method
/// <summary>
/// Converts a set of screen point/etc. arguments to ADF geometry for selection.
/// </summary>
/// <param name="args">ToolEventArgs object, which may be any one of derived types
/// (PointEventArgs, RectangleEventArgs, etc.)</param>
/// <param name="mapControl">Map control with envelope etc. information</param>
/// <param name="mapDimensions">Map dimensions, stored as a Point with x
/// containing width and y containing height</param>
/// <param name="tolerance">For point argument, tolerance in pixels for selection around
/// the point (cannot select from point/line layers unless add tolerance).</param>
/// <returns></returns>
public static Geometry ToAdfGeometry(ToolEventArgs args,
Map mapControl, int tolerance)
{
// degrees between points in simulated circle
double selectCircleDegreeIncrement = 10;
Geometry selectGeometry = null;
double circleRadius = -1;
// Map height/width only guaranteed to be up to date in
// TilingScheme -- Map.Width may be set to a percentage, which won't work
int mapWidth = mapControl.TilingScheme.ViewWidth;
int mapHeight = mapControl.TilingScheme.ViewHeight;
if (args is PointEventArgs)
{
PointEventArgs pointArgs = (PointEventArgs)args;
System.Drawing.Point screenPt = pointArgs.ScreenPoint;
if (tolerance <= 0)
{
ESRI.ArcGIS.ADF.Web.Geometry.Point adfPt =
ESRI.ArcGIS.ADF.Web.Geometry.Point.ToMapPoint(
screenPt, mapControl.Extent, mapControl.TilingScheme.ViewWidth,
mapControl.TilingScheme.ViewHeight);
selectGeometry = adfPt;
}
else
{
// construct envelope around selection point
int tolRadius = (int)Math.Round((double)tolerance / 2);
if (tolRadius <= 0) tolRadius = 1;
Envelope ptEnv = Envelope.ToMapEnvelope(screenPt.X - tolRadius,
screenPt.Y - tolRadius, screenPt.X + tolRadius,
screenPt.Y + tolRadius, mapControl.Extent,
mapWidth, mapHeight);
selectGeometry = ptEnv;
}
}
else if (args is RectangleEventArgs)
{
RectangleEventArgs rectArgs = (RectangleEventArgs)args;
System.Drawing.Rectangle screenRect = rectArgs.ScreenExtent;
ESRI.ArcGIS.ADF.Web.Geometry.Envelope adfEnvelope =
ESRI.ArcGIS.ADF.Web.Geometry.Envelope.ToMapEnvelope(
screenRect, mapControl.Extent, mapControl.TilingScheme.ViewWidth,
mapControl.TilingScheme.ViewHeight);
selectGeometry = adfEnvelope;
}
else if (args is CircleEventArgs)
{
CircleEventArgs circleArgs = (CircleEventArgs)args;
ScreenPoint screenPt = circleArgs.CenterPoint;
ESRI.ArcGIS.ADF.Web.Geometry.Point adfPt =
ESRI.ArcGIS.ADF.Web.Geometry.Point.ToMapPoint(
(int)screenPt.X, (int)screenPt.Y, mapControl.Extent,
mapWidth, mapHeight);
selectGeometry = adfPt;
// create temporary point to calculate map circle radius
ESRI.ArcGIS.ADF.Web.Geometry.Point tmpPt =
ESRI.ArcGIS.ADF.Web.Geometry.Point.ToMapPoint(
(int)(screenPt.X + circleArgs.Radius), (int)screenPt.Y,
mapControl.Extent,
mapWidth, mapHeight);
circleRadius = Math.Abs(tmpPt.X - adfPt.X);
Polygon polyCircle = CreateCirclePolygon(adfPt, circleRadius,
selectCircleDegreeIncrement);
selectGeometry = polyCircle;
}
else if (args is PolylineEventArgs)
{
PolylineEventArgs plArgs = (PolylineEventArgs)args;
System.Drawing.Point[] vectors = plArgs.Vectors;
Polyline adfPolyline = new Polyline();
Path path = new Path();
path.Points = Utilities.ToAdfPointCollection(
vectors, mapControl, mapWidth, mapHeight, false);
adfPolyline.Paths.Add(path);
selectGeometry = adfPolyline;
}
else if (args is PolygonEventArgs)
{
PolygonEventArgs pArgs = (PolygonEventArgs)args;
System.Drawing.Point[] vectors = pArgs.Vectors;
Polygon adfPolygon = new Polygon();
Ring ring = new Ring();
ring.Points = Utilities.ToAdfPointCollection(
vectors, mapControl, mapWidth, mapHeight, true);
adfPolygon.Rings.Add(ring);
selectGeometry = adfPolygon;
}
return selectGeometry;
}
#endregion
#region ToAdfPointCollection
private static PointCollection ToAdfPointCollection(System.Drawing.Point[] vectors,
Map map, int mapWidth, int mapHeight, bool closePolygon)
{
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -