【问题标题】:How to create Polygon using 4 points?如何使用 4 个点创建多边形?
【发布时间】:2019-06-10 08:35:24
【问题描述】:

我有 CSV 文件,其中包含点的坐标(超过 100 行)。在 CSV 文件中有 2 列:纬度、经度。
这些点是某些多边形的左上角。 (正方形)
所有多边形的大小都相同(例如 100x100 米)。

纬度经度 56.37769816725615 -4.325049868061924 55.37769816725615 -3.325049868061924 51.749167440074324 -4.963575226888083 ...

我可以将 CSV 加载到数据框,我可以使用 GeoPandas 从坐标中创建点(或行内的 4 个点)。
但是如何为每一行制作多边形,连接 4 个点?

感谢您的帮助。

df = pd.read_csv('ExportPolyID.csv',nrows=10)
gdf= geopandas.GeoDataFrame(df,geometry=geopandas.points_from_xy(df.long, df.lat))
gdf['point2']= gdf.translate(2,2)
gdf['point3']=gdf.translate(3,3)
gdf['point4']=gdf.translate(4,4)
#After this I have 4 points for each row, but I can't connect them to create Polygons

【问题讨论】:

    标签: python python-3.x pandas polygon geopandas


    【解决方案1】:

    如果您想以米为单位定义平方,请确保您使用的是投影 CRS (http://geopandas.org/projections.html#re-projecting)。

    然后你可以使用这样的东西(可能有更有效的方法,但这个是明确的):

    from shapely.geometry import Polygon
    lat = [0, 2, 4]
    lon = [0, 2, 4]
    
    gdf = gpd.GeoDataFrame()
    gdf['lat'] = lat
    gdf['lon'] = lon
    
    dim = 1  # define the length of the side of the square
    geoms = []
    for index, row in gdf.iterrows():
        ln = row.lon
        lt = row.lat
        geom = Polygon([(ln, lt), ((ln + dim), lt), ((ln + dim), (lt - dim)), (ln, (lt - dim))])
        geoms.append(geom)
    
    gdf['geometry'] = geoms 
    

    这将从大小为dim x dim 的设置坐标生成正方形多边形,由给定坐标定义的点位于左上角。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2019-08-10
      • 2020-06-07
      • 1970-01-01
      • 2019-10-14
      • 2019-07-20
      • 1970-01-01
      • 2012-03-13
      相关资源
      最近更新 更多