从布局视图中的当前数据框范围创建shapefile的ArcGIS 10工具在哪里?
环顾四周,我可以找到的壁橱是Data Drive Pages下的Toolbox的Grid / Strip Map Index工具。
我只希望能够为任何给定的比例/页面设置基于数据框(在布局视图中)创建单个多边形矩形shp文件。
从布局视图中的当前数据框范围创建shapefile的ArcGIS 10工具在哪里?
环顾四周,我可以找到的壁橱是Data Drive Pages下的Toolbox的Grid / Strip Map Index工具。
我只希望能够为任何给定的比例/页面设置基于数据框(在布局视图中)创建单个多边形矩形shp文件。
Answers:
此c#代码可用于构建Arcmap的外接程序。
using System;
using System.Collections.Generic;
using System.Text;
using System.IO;
using System.Windows.Forms;
using ESRI.ArcGIS.Geometry;
using ESRI.ArcGIS.Carto;
using ESRI.ArcGIS.ArcMapUI;
using ESRI.ArcGIS.DataSourcesFile;
using ESRI.ArcGIS.Geodatabase;
namespace MainToolsAddin
{
public class Extent2ShapefileButton : ESRI.ArcGIS.Desktop.AddIns.Button
{
public Extent2ShapefileButton()
{
}
protected override void OnClick()
{
try
{
var polygon = GetExtentPolygon(ArcMap.Document.FocusMap);
//IGraphicsContainer gc = ArcMap.Document.FocusMap as IGraphicsContainer;
//var element = new PolygonElementClass() as IElement;
//element.Geometry = polygon;
//((IFillShapeElement)element).Symbol = ((IDocumentDefaultSymbols)ArcMap.Document).FillSymbol;
//gc.AddElement(element,0);
//((IActiveView)ArcMap.Document.FocusMap).Refresh();
WritePolygon(@"C:\projects\forums\extents.shp", polygon);
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
}
}
protected override void OnUpdate()
{
}
private void WritePolygon(string shpFilePath, IGeometry geom)
{
var featClass = OpenShapeFile(shpFilePath);
if (featClass == null)
featClass = CreateShapeFile(shpFilePath, geom);
IFeature feat = featClass.CreateFeature();
feat.Shape = geom;
feat.Store();
}
private IFeatureClass CreateShapeFile(string shpFilepath, IGeometry geom)
{
System.IO.FileInfo fi = new FileInfo(shpFilepath);
var wsf = Activator.CreateInstance(Type.GetTypeFromProgID("esriDataSourcesFile.ShapefileWorkspaceFactory")) as IWorkspaceFactory;
var fws = wsf.OpenFromFile(fi.DirectoryName, 0) as IFeatureWorkspace;
IFieldsEdit flds = new FieldsClass();
flds.AddField(MakeField("ObjectID", esriFieldType.esriFieldTypeOID,0));
IGeometryDefEdit geomDef = new GeometryDefClass();
geomDef.GeometryType_2 = geom.GeometryType;
geomDef.SpatialReference_2 = geom.SpatialReference;
var shpField = MakeField("Shape", esriFieldType.esriFieldTypeGeometry, 0) as IFieldEdit;
shpField.GeometryDef_2 = geomDef;
flds.AddField(shpField);
flds.AddField(MakeField("Name", esriFieldType.esriFieldTypeString, 16));
string fcName = fi.Name;
if (fcName.ToUpper().EndsWith(".SHP"))
fcName = fcName.Substring(0, fcName.LastIndexOf("."));
var fc = fws.CreateFeatureClass(fcName, flds, null, null, esriFeatureType.esriFTSimple, "Shape", "");
return fc;
}
private IField MakeField(string name, esriFieldType fType, int length)
{
IFieldEdit fld = new FieldClass();
fld.Name_2 = name;
fld.Type_2 = fType;
if (length > 0 && fType == esriFieldType.esriFieldTypeString)
fld.Length_2 = length;
return fld;
}
private IFeatureClass OpenShapeFile(string shpFilepath)
{
var wsf = Activator.CreateInstance(Type.GetTypeFromProgID("esriDataSourcesFile.ShapefileWorkspaceFactory")) as IWorkspaceFactory;
System.IO.FileInfo fi = new FileInfo(shpFilepath);
string name = fi.Name.ToUpper().EndsWith(".SHP") ? fi.Name.Substring(0, fi.Name.LastIndexOf(".")) : fi.Name;
string fileName = String.Format("{0}.shp", name);
if (File.Exists(System.IO.Path.Combine(fi.DirectoryName,fileName)))
{
var fws = wsf.OpenFromFile(fi.DirectoryName, 0) as IFeatureWorkspace;
return fws.OpenFeatureClass(name);
}
else
return null;
}
private IPolygon GetExtentPolygon(IMap map)
{
// A polygon is returned since the dataframe might be rotated
var grphCont = ArcMap.Document.PageLayout as IGraphicsContainer;
var mapFrame = grphCont.FindFrame(map) as IMapFrame;
var av = map as IActiveView;
var extent = mapFrame.MapBounds.Envelope;
ISegmentCollection sc = new PolygonClass() as ISegmentCollection;
sc.SetRectangle(extent);
var center = ((IArea)extent).Centroid;
var angle = -(av.ScreenDisplay.DisplayTransformation.Rotation / 180.0 * Math.PI);
((ITransform2D)sc).Rotate(center, angle);
return (IPolygon)sc;
}
}
}
当您使用Visual Studio创建新的外接程序项目时,应该会看到一些类似的选项。我不确定它是否适用于Visual Studio Express,或者是否需要安装ArcObjects SDK。
这是一个基本的python脚本,用于根据数据框范围创建多边形。调整变量以适合您的需求。如果只需要一个简单的范围多边形,则可以摆脱“ feat”,“ scale”和“ Page”。(“页面”仅在使用数据驱动页面时才有效)。
doc = arcpy.mapping.MapDocument("current")
df = arcpy.mapping.ListDataFrames(doc)[0] #First Dataframe
extent = df.extent
fc = arcpy.GetParameterAsText(0)
feat = arcpy.GetParameterAsText(1)
scale = arcpy.GetParameterAsText(2)
Page = doc.dataDrivenPages.currentPageID
# Create Extent Polygon
array = arcpy.Array()
array.add(extent.lowerLeft)
array.add(extent.lowerRight)
array.add(extent.upperRight)
array.add(extent.upperLeft)
array.add(extent.lowerLeft)
polygon = arcpy.Polygon(array)
cursor = arcpy.da.InsertCursor(fc,["SHAPE@","Page","Feature","Scale"])
cursor.insertRow([polygon, Page, feat, scale])
del cursor
我看到这个NOAA门户网站有一个程序。
脚本-shapefile的范围
我知道我在某处看到过该脚本。还在寻找。
这是资源中心上的几个ddp工具。