如皋项目开发基本结束,终于有时间总结整理下老。
首先从最基本的查询开始,先写空间查询部分。理下思路:选择绘图工具(点、线、框、面)-->操作地图-->高亮显示地物-->列出查询结果对话框-->单击某条记录,高亮居中显示-->地图清屏
(1)选择绘图工具,操作地图。
之前做server api for flex用到Tool 、Symbol、GraphicsLayer等object,定义绘图工具的代码示例如下:
<esri:Draw id="myDrawToolbar" map="{myMap}" graphicsLayer="{myGraphicsLayer}" markerSymbol="{sms}" lineSymbol="{sls}" fillSymbol="{sfs}" />
跟这相比,AE开发相同他之处是同样要定义颜色IRgbColor、符号ISimpleMarkerSymbol;存在的差异有用IElement(IMarkerElement、ILineElement,ect)作为添加的对象,容器则是IGraphicsContainer。示例代码如下:
(2)高亮显示地物。
高亮显示地物,用到IMap的SelectByShape方法;
C#定义:public void SelectByShape ( IGeometry Shape, ISelectionEnvironment env, bool justOne );
根据参数的要求,需要有IGeometry对象,这个来自于,上面地图操作中绘制的图形(点、线、面、框)。一般基于点查询会设置一定范围的矩形进行查询,代码如下:
下面根据IGeometry进行查询高亮显示,SelectByShape方法只针对IMap中可选图层,所以可以首先控制图层是否可选,然后再进行调用此方法。
补充下,设置某类图层不可选的代码:
地图查询实质就是对每个可选图层进行查询。直接上code:
地图查询://FeatureSelection of all selectable featurelayers(Array of LayerSelection)
某个图层查询://FeatureSelection of the single featurelayer(layername:oid1,oid2,...)
(3)在进行高亮显示的同时已经将地图信息进行统计,一般用树结构分图层展示,主要是树结构的创建与控制事件的实现。
创建树结构的代码:
首先从最基本的查询开始,先写空间查询部分。理下思路:选择绘图工具(点、线、框、面)-->操作地图-->高亮显示地物-->列出查询结果对话框-->单击某条记录,高亮居中显示-->地图清屏
(1)选择绘图工具,操作地图。
之前做server api for flex用到Tool 、Symbol、GraphicsLayer等object,定义绘图工具的代码示例如下:
<esri:Draw id="myDrawToolbar" map="{myMap}" graphicsLayer="{myGraphicsLayer}" markerSymbol="{sms}" lineSymbol="{sls}" fillSymbol="{sfs}" />
跟这相比,AE开发相同他之处是同样要定义颜色IRgbColor、符号ISimpleMarkerSymbol;存在的差异有用IElement(IMarkerElement、ILineElement,ect)作为添加的对象,容器则是IGraphicsContainer。示例代码如下:
IGraphicsContainer pGraphicsContainer;
IRubberBand pRubberBand;
IRgbColor pRgbColor = new RgbColorClass();
pRgbColor.Red = 255;
pRgbColor.Green = 0;
pRgbColor.Blue = 0;
ISimpleMarkerSymbol pSimpleMarkerSymbol = new SimpleMarkerSymbolClass();
pSimpleMarkerSymbol.Color = pRgbColor;
pSimpleMarkerSymbol.Size = 3;
IMarkerElement pMarkerElement;
IElement pMElement;
IPoint pPoint;
pMarkerElement = new MarkerElementClass();
pMarkerElement.Symbol = pSimpleMarkerSymbol as ISimpleMarkerSymbol;
pMElement = pMarkerElement as IElement;
pRubberBand = new RubberPointClass();
pPoint = pRubberBand.TrackNew(axMapControl1.ActiveView.ScreenDisplay, null) as IPoint;
pMElement.Geometry = pPoint;
pGraphicsContainer = axMapControl1.ActiveView as IGraphicsContainer;
pGraphicsContainer.AddElement(pMElement, 0);
axMapControl1.ActiveView.PartialRefresh(esriViewDrawPhase.esriViewGraphics, null, null);
(2)高亮显示地物。
高亮显示地物,用到IMap的SelectByShape方法;
C#定义:public void SelectByShape ( IGeometry Shape, ISelectionEnvironment env, bool justOne );
根据参数的要求,需要有IGeometry对象,这个来自于,上面地图操作中绘制的图形(点、线、面、框)。一般基于点查询会设置一定范围的矩形进行查询,代码如下:
IEnvelope pEnvelope = new EnvelopeClass();
pEnvelope.XMin = pPoint.X - 10;
pEnvelope.XMax = pPoint.X + 10;
pEnvelope.YMin = pPoint.Y - 10;
pEnvelope.YMax = pPoint.Y + 10;
pGeometry = pEnvelope as IGeometry;
下面根据IGeometry进行查询高亮显示,SelectByShape方法只针对IMap中可选图层,所以可以首先控制图层是否可选,然后再进行调用此方法。
补充下,设置某类图层不可选的代码:
public bool UnselectableDLG(ref AxMapControl axMapControl)
{
ILayer pLayer = null;
ICompositeLayer pCompositeLayer = null;
IFeatureLayer pFeatureLayer = null;
int iLayerCount;
try
{
iLayerCount = axMapControl.ActiveView.FocusMap.LayerCount;
if (iLayerCount > 0)
{
for (int i = 0; i < iLayerCount; i++)
{
pLayer = axMapControl.ActiveView.FocusMap.get_Layer(i);
if (pLayer is IGroupLayer)
{
pCompositeLayer = pLayer as ICompositeLayer;
for (int j = 0; j < pCompositeLayer.Count; j++)
{
if (pCompositeLayer.get_Layer(j) is IFeatureLayer)
{
pFeatureLayer = pCompositeLayer.get_Layer(j) as IFeatureLayer;
if (pFeatureLayer.Name.Contains("DLG"))
{
pFeatureLayer.Selectable = false;
}
}
}
}
else if (pLayer is IFeatureLayer)
{
pFeatureLayer = pLayer as IFeatureLayer;
if (pFeatureLayer.Name.Contains("DLG"))
{
pFeatureLayer.Selectable = false;
}
}
}
return true;
}
else
{
if (pLayer != null)
Marshal.ReleaseComObject(pLayer);
if (pFeatureLayer != null)
Marshal.ReleaseComObject(pFeatureLayer);
if (pCompositeLayer != null)
Marshal.ReleaseComObject(pCompositeLayer);
return false;
}
if (pLayer != null)
Marshal.ReleaseComObject(pLayer);
if (pFeatureLayer != null)
Marshal.ReleaseComObject(pFeatureLayer);
if (pCompositeLayer != null)
Marshal.ReleaseComObject(pCompositeLayer);
return false;
}
catch (System.Exception ex)
{
if (pLayer != null)
Marshal.ReleaseComObject(pLayer);
if (pFeatureLayer != null)
Marshal.ReleaseComObject(pFeatureLayer);
if (pCompositeLayer != null)
Marshal.ReleaseComObject(pCompositeLayer);
return false;
}
}
地图查询实质就是对每个可选图层进行查询。直接上code:
地图查询://FeatureSelection of all selectable featurelayers(Array of LayerSelection)
public List<string> MapSelection(IMap pMap,IGeometry pGeometry)
{
List<string> lstRecord = null;
string temp=string.Empty;
ILayer pLayer = null;
ICompositeLayer pCompositeLayer = null;
IFeatureLayer pFeatureLayer = null;
int iLayerCount;
try
{
lstRecord = new List<string>();
iLayerCount = pMap.LayerCount;
if (iLayerCount>0)
{
for (int i = 0; i < iLayerCount; i++)
{
pLayer = pMap.get_Layer(i);
if (pLayer is IGroupLayer)
{
pCompositeLayer = pLayer as ICompositeLayer;
for (int j = 0; j < pCompositeLayer.Count; j++)
{
if (pCompositeLayer.get_Layer(j) is IFeatureLayer)
{
pFeatureLayer = pCompositeLayer.get_Layer(j) as IFeatureLayer;
if (pFeatureLayer.Selectable)
{
temp = LayerSelection(pFeatureLayer, pGeometry);
if (!temp.StartsWith("err:"))
lstRecord.Add(temp);
else
{
lstRecord.Add(temp + "@图层:" + pFeatureLayer.Name);
return lstRecord;
}
}
}
}
}
else if (pLayer is IFeatureLayer)
{
pFeatureLayer = pLayer as IFeatureLayer;
if (pFeatureLayer.Selectable)
{
temp = LayerSelection(pFeatureLayer, pGeometry);
if (!temp.StartsWith("err:"))
lstRecord.Add(temp);
else
{
lstRecord.Add(temp + "@图层:" + pFeatureLayer.Name);
return lstRecord;
}
}
}
}
}else
{
temp = "err:没有图层.";
lstRecord.Add(temp);
return lstRecord;
}
if (pLayer != null)
Marshal.ReleaseComObject(pLayer);
if (pFeatureLayer != null)
Marshal.ReleaseComObject(pFeatureLayer);
if (pCompositeLayer != null)
Marshal.ReleaseComObject(pCompositeLayer);
return lstRecord;
}
catch (System.Exception ex)
{
if (pLayer != null)
Marshal.ReleaseComObject(pLayer);
if (pFeatureLayer != null)
Marshal.ReleaseComObject(pFeatureLayer);
if (pCompositeLayer != null)
Marshal.ReleaseComObject(pCompositeLayer);
temp = "err:"+ex.Message;
lstRecord.Add(temp);
return lstRecord;
}
}
某个图层查询://FeatureSelection of the single featurelayer(layername:oid1,oid2,...)
public string LayerSelection(IFeatureLayer pFeatureLayer,IGeometry pGeometry)
{
string strRecords = pFeatureLayer.Name + ":";
string displayField = string.Empty;
IFeatureClass pFeatureClass = null;
ISpatialFilter pSpatialFilter = null;
IFeatureCursor pFeatureCursor = null;
IFeature pFeature = null;
try
{
pFeatureClass = pFeatureLayer.FeatureClass;
pSpatialFilter = new SpatialFilterClass();
pSpatialFilter.Geometry = pGeometry;
pSpatialFilter.SpatialRel = esriSpatialRelEnum.esriSpatialRelIntersects;
pSpatialFilter.GeometryField = pFeatureClass.ShapeFieldName;
pFeatureCursor = pFeatureClass.Search(pSpatialFilter, false);
pFeature = pFeatureCursor.NextFeature();
while (pFeature != null)
{
strRecords += pFeature.get_Value(pFeature.Fields.FindField("OBJECTID")).ToString()+",";
pFeature = pFeatureCursor.NextFeature();
}
if (pFeature != null)
Marshal.ReleaseComObject(pFeature);
if (pFeatureCursor != null)
Marshal.ReleaseComObject(pFeatureCursor);
if(pSpatialFilter!=null)
Marshal.ReleaseComObject(pSpatialFilter);
if (pFeatureClass != null)
Marshal.ReleaseComObject(pFeatureClass);
strRecords = strRecords.Substring(0, strRecords.Length - 1);
return strRecords;
}
catch (System.Exception ex)
{
if (pFeature != null)
Marshal.ReleaseComObject(pFeature);
if (pFeatureCursor != null)
Marshal.ReleaseComObject(pFeatureCursor);
if (pSpatialFilter != null)
Marshal.ReleaseComObject(pSpatialFilter);
if (pFeatureClass != null)
Marshal.ReleaseComObject(pFeatureClass);
strRecords = "err:" + ex.Message;
return strRecords;
}
}
(3)在进行高亮显示的同时已经将地图信息进行统计,一般用树结构分图层展示,主要是树结构的创建与控制事件的实现。
创建树结构的代码:
TreeNode pNode;
TreeNode pParent;
string[] temp;
string[] detail;
try
{
for (int i = 0; i < lstReturn.Count; i++)
{
temp= lstReturn[i].Split(':');
pParent = new TreeNode(temp[0]);
treeResult.Nodes.Add(pParent);
if(temp.Length==2)
{
detail = temp[1].Split(',');
foreach (string obj in detail)
{
pNode = new TreeNode(obj);
pParent.Nodes.Add(pNode);
}
}
}
}
catch (System.Exception ex)
{
MessageBox.Show("err:"+ex.Message);
}