Answers:
当ArcGIS 10.0是最新软件时,大多数其他答案都已发布。在ArcGIS 10.1中,许多新的ArcPy功能可用。该答案利用了该新功能。它不适用于10.0,但为10.1和更高版本提供了增强的性能和功能。
import arcpy
input_feature_class = 'C:\your_feature_class.shp'
wkid = 4326 # wkid code for wgs84
spatial_reference = arcpy.SpatialReference(wkid)
fields_to_work_with = ['SHAPE@']
with arcpy.da.SearchCursor(input_feature_class,
fields_to_work_with) as s_cur:
for row in s_cur:
point_in_wgs84 = row[0].projectAs(spatial_reference)
print point_in_wgs84.firstPoint.X, point_in_wgs84.firstPoint.Y
此代码段使用WKID来创建空间参考对象,而不是打字出字符串表示,使用了更现代的数据访问游标,和项目的个别几何使用projectAs()方法的对象。
为了详细说明James的建议,以下是使用Python / arcpy的最小代码示例:
import arcpy
def main():
projectedPointFC = r'c:\point_test.shp'
desc = arcpy.Describe(projectedPointFC)
shapefieldname = desc.ShapeFieldName
rows = arcpy.SearchCursor(projectedPointFC, r'', \
r'GEOGCS["GCS_WGS_1984",' + \
'DATUM["D_WGS_1984",' + \
'SPHEROID["WGS_1984",6378137,298.257223563]],' + \
'PRIMEM["Greenwich",0],' + \
'UNIT["Degree",0.017453292519943295]]')
for row in rows:
feat = row.getValue(shapefieldname)
pnt = feat.getPart()
print pnt.X, pnt.Y
if __name__ == '__main__':
main()
无论您是否称其为投影,我都可以肯定地说,根据定义,当您将坐标值从一个空间参照系转换到另一个空间参照系时,您就是在重新/不进行投影。
我对ArcPy不太熟悉,但是在9.3版的arcgisscripting中,您将必须投影整个要素类。
根据所需的投影/转换算法的复杂程度,您始终可以在基本的python数学中滚动自己的坐标投影。这将允许您在要素级别协调值投影。
如果您愿意使用OGR python绑定,则可以在功能级别(例如“搜索光标”)中进行投影。
我看到不希望创建要素类的主要原因是因为arcpy.CreateFeatureclass_management速度很慢。您还可以使用arcpy.da.NumPyArrayTofeatureClass,它对于in_memory要素类或多或少是即时的:
In [1]: import arcpy
In [2]: import numpy as np
In [3]: geosr = arcpy.SpatialReference('Geographic Coordinate Systems/Spheroid-based/WGS 1984 Major Auxiliary Sphere')
In [4]: tosr = arcpy.SpatialReference('Projected Coordinate Systems/World/WGS 1984 Web Mercator (auxiliary sphere)')
In [5]: npai=list(enumerate(((-115.12799999956881, 36.11419999969922), (-117, 38.1141))))
In [6]: npai
Out[6]: [(0, (-115.12799999956881, 36.11419999969922)), (1, (-117, 38.1141))]
In [7]: npa=np.array(npai, np.dtype(([('idfield', np.int32), ('XY', np.float, 2)])))
In [8]: npa
Out[8]:
array([(0, [-115.12799999956881, 36.11419999969922]),
(1, [-117.0, 38.1141])],
dtype=[('idfield', '<i4'), ('XY', '<f8', (2,))])
In [9]: fcName = arcpy.CreateScratchName(workspace='in_memory', data_type='FeatureClass')
In [10]: arcpy.da.NumPyArrayToFeatureClass(npa, fcName, ['XY'], geosr)
In [11]: with arcpy.da.SearchCursor(fcName, 'SHAPE@XY', spatial_reference=tosr) as cur:
...: print list(cur)
...:
[((-12815990.336048, 4316346.515041453),), ((-13024380.422813002, 4595556.878958654),)]
import arcpy
dsc = arcpy.Describe(FC)
cursor = arcpy.UpdateCursor(FC, "", "Coordinate Systems\Geographic Coordinate Systems\World\WGS 1984.prj")
for row in cursor:
shape=row.getValue(dsc.shapeFieldName)
geom = shape.getPart(0)
x = geom.X
y = geom.Y
row.setValue('LONG_DD', x)
row.setValue('LAT_DD', y)
cursor.updateRow(row)
del cursor, row