簡體   English   中英

如何將坐標轉換為 shapefile

[英]How to convert coordinates to a shapefile

我有下面發布的坐標,但在 wkt 格式中,我想寫入一個文件作為 shapefile。當我運行代碼時,我收到錯誤:

TypeError: object of type 'float' has no len()

在放置一些用於調試的日志后,我發現方法 bindPolygonCoordinates bindPolygonCoordinates返回任何內容,請告訴我如何正確地將坐標導出到 shapefile。

代碼

def extractLonLatFromPolygonInWKTFor(polygonInWKT):
lons = []
lats = []
s = polygonInWKT.replace("POLYGON","")
s = s.replace("((","")
s = s.replace("))","")
s = s.strip()
lonsLats = s.split(",")
for i in range (0,len(lonsLats)):
    lonLat = lonsLats[i].strip()
    lonLat = lonLat.split(" ")
    lons.append(float(lonLat[0]))
    lats.append(float(lonLat[1]))
return lons,lats

def bindPolygonCoordinates(longitudeValuesArray, latitudeValuesArray):
return Polygon(zip(longitudeValuesArray, latitudeValuesArray))

def buildGeoDataFrameForGeometry(geometry):
crs = {'init': 'epsg:4326'}
return gpd.GeoDataFrame(index=[0], crs=crs, geometry=[geometry])

lons,lats = extractLonLatFromPolygonInWKTFor(fieldCoordinatesAsTextInWKTInEPSG4326)
boundingPolygonGeometry = bindPolygonCoordinates(lons, lats)#returns nothing
boundingGeometryAsGDF = buildGeoDataFrameForGeometry(boundingPolygonGeometry)

坐標

fieldCoordinatesAsTextInWKTInEPSG4326:POLYGON((6.692790084436616 51.13237486727857,6.6918971115756305 51.132725423664596,6.6922145189906725 51.13301489625002,6.6926758177672 51.13291397940796,6.692650425173997 51.1327121450621,6.692430356032901 51.132520932762816,6.692790084436616 51.13237486727857))

lonsLats:['6.741879696309871 51.08423775429969', '6.742907378503366 51.08158745820981', '6.746964018740842 51.08233499299334', '6.746152690693346 51.08440763989611', '6.741879696309871 51.08423775429969']
  • 根據其他評論,使用shapely.wky.loads()
  • 無需手工代碼解析標准格式
  • 生成.shp文件然后由geopandas完成
import shapely.wkt
import geopandas as gpd
from pathlib import Path

fieldCoordinatesAsTextInWKTInEPSG4326 = "POLYGON((6.692790084436616 51.13237486727857,6.6918971115756305 51.132725423664596,6.6922145189906725 51.13301489625002,6.6926758177672 51.13291397940796,6.692650425173997 51.1327121450621,6.692430356032901 51.132520932762816,6.692790084436616 51.13237486727857))"

f = Path.cwd().joinpath("shape_dir")
if not f.is_dir(): f.mkdir()
f = f.joinpath("shape.shp")
gpd.GeoDataFrame(geometry=[shapely.wkt.loads(fieldCoordinatesAsTextInWKTInEPSG4326)]).to_file(str(f))

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM