如何将坐标转换为 shapefile

How to convert coordinates to a shapefile

我有以下发布的坐标,但在 wkt formate 中,我想将文件写入 shapefile.when 我 运行 我收到错误代码:

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

放置一些日志调试后,我发现,方法bindPolygonCoordinates returns没什么 请告诉我如何正确地将坐标导出到 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))