【ArcPy】如何在ArcPy创建要素中生成精准的XY坐标?解决精度损失问题

发布时间 2023-04-16 17:06:07作者: yzhyingcool

使用ArcPy创建要素的代码段前面有发布,【arcpy】创建点、线、面(孔洞、环、多部件)要素、要素类

Q:这些代码里创建要素后会存在XY精度损失的问题,如何解决?

A:解决方案是在创建要素过程中指定正确的空间参考。

答案来自 geometry - How to handle coordinates accuracy in ArcGIS - Geographic Information Systems Stack Exchange

原文如下:

This observation goes back to 2010. If a spatial reference isn't specified, then you will get lower precision results. See https://geonet.esri.com/thread/10256 for starters. The examples are more extensive. There are even more in subsequent years. Specifying a floating point input has no impact, so rule that out as a thought.

>>> import arcpy
>>> corners =[[0.0,0.0],[0.0,1.0],[1.0,1.0],[1.0,0.0],[0.0,0.0]]
>>> p=arcpy.Polygon(arcpy.Array([arcpy.Point(*coords) for coords in corners]))
>>> p.area
1.0002441555261612
>>> # hmmmm close... try again
>>> SR = arcpy.SpatialReference(3395) # WGS_1984_ World_Mercator_3395.prj doesn't really matter
>>> p=arcpy.Polygon(arcpy.Array([arcpy.Point(*coords) for coords in corners]),SR)
>>> p.area
1.0

How about pure Python? (very simple example obviously)

def polygon_area(pnts):  
  '''determine the area given a list of points'''  
  area = 0.0;  n = len(pnts)  
  j = n - 1;  i = 0  
  for point in pnts:  
    p0 = pnts[i];  p1 = pnts[j]  
    area += p0[0] * p1[1]  
    area -= p0[1] * p1[0]  
    j = i; i += 1  
  area /= 2.0  
  return area 

if __name__=='__main__':
  square = [[0,0],[0,1],[1,1],[1,0]]  
  print('Unit square area: {}'.format(polygon_area(square))) 

result...

>>> Unit square area: 1.0