如何用Python将圆形坐标写入Shapefile?新手技术求助
将圆形经纬度坐标转换为多边形Shapefile的解决方案
问题说明
作为Python新手,我持有一组圆形经纬度坐标(示例如下),需要转换为多边形Shapefile。尝试pyshp官方测试代码及修改后的代码均失败,报错信息:TypeError: poly() got an unexpected keyword argument 'parts',不清楚问题根源,也不知道如何适配自己的坐标数据。
示例坐标
circle_coords = [(39.1852751477193, -4.661967433717266), (39.185270807935424, -4.6620560715526205), (39.18525783015025, -4.6621438557678925), (39.18523633934382, -4.662229940952642), (39.18520654248149, -4.66231349805872), (39.185168726520814, -4.662393722384509), (39.1851232556481, -4.662469841324661), (39.185070567771184, -4.662541121810758), (39.18501117030217, -4.6626068773712275), (39.184945635270836, -4.662666474742481), (39.184874593815685, -4.662719339967662), (39.1847987301057, -4.662764963924225), (39.18471877475142, -4.662802907227118), (39.18463549776867, -4.662832804460353), (39.18454970116283, -4.662854367696218), (39.184462211205, -4.662867389268209), (39.1843738704744, -4.662871743771006), (39.1842855297438, -4.662867389268209), (39.184198039785976, -4.662854367696218), (39.18411224318014, -4.662832804460353), (39.18402896619739, -4.662802907227118), (39.18394901084311, -4.662764963924225), (39.18387314713312, -4.662719339967661), (39.18380210567796, -4.662666474742481), (39.18373657064664, -4.6626068773712275), (39.18367717317762, -4.662541121810758), (39.183624485300705, -4.662469841324661), (39.18357901442799, -4.662393722384509), (39.18354119846732, -4.662313498058719), (39.18351140160499, -4.662229940952642), (39.183489910798556, -4.6621438557678925), (39.18347693301339, -4.6620560715526205), (39.1834725932295, -4.661967433717266), (39.18347693323819, -4.661878795892798), (39.183489911239526, -4.661791011709759), (39.183511402245166, -4.661704926577361), (39.18354119928211, -4.661621369541729), (39.18357901538609, -4.66154114530178), (39.18362448636528, -4.661465026459563), (39.18367717430777, -4.661393746079731), (39.183736571798924, -4.661327990629777), (39.183802106808116, -4.6612683933690375), (39.1838731481977, -4.661215528250123), (39.183949011801204, -4.661169904391494), (39.18402896701218, -4.661131961174444), (39.184112243820316, -4.661102064011655), (39.18419804022694, -4.661080500828139), (39.184285529968605, -4.661067479288384), (39.1843738704744, -4.6610631247964704), (39.1844622109802, -4.661067479288384), (39.18454970072187, -4.66108050082814), (39.18463549712849, -4.661102064011656), (39.18471877393662, -4.661131961174444), (39.1847987291476, -4.661169904391494), (39.18487459275111, -4.661215528250122), (39.18494563414069, -4.6612683933690375), (39.18501116914988, -4.661327990629778), (39.18507056664104, -4.661393746079731), (39.18512325458352, -4.661465026459563), (39.18516872556272, -4.66154114530178), (39.185206541666695, -4.661621369541729), (39.18523633870364, -4.66170492657736), (39.18525782970928, -4.661791011709759), (39.185270807710616, -4.661878795892798), (39.1852751477193, -4.661967433717266)]
尝试过的代码及报错
最初测试代码:
import shapefile w = shapefile.Writer(shapefile.POLYGON) w.poly(parts=[[[1,5],[5,5],[5,1],[3,3],[1,1]]]) w.field('FIRST_FLD','C','40') w.field('SECOND_FLD','C','40') w.record('First','Polygon') w.save('shapefiles/test/polygon')
修改后的代码:
import shapefile w = shapefile.Writer('shapefiles/test/polygon') w.poly(parts=[[[1,5],[5,5],[5,1],[3,3],[1,1]]]) w.field('FIRST_FLD','C','40') w.field('SECOND_FLD','C','40') w.record('First','Polygon') w.close()
报错信息:
TypeError: poly() got an unexpected keyword argument 'parts'
错误原因
pyshp(shapefile库)的版本差异导致API变化:新版本(2.x及以上)中,poly()方法不再支持parts关键字参数,而是直接接收多边形的坐标列表,或者推荐使用更直观的polygon()方法。另外,Shapefile的坐标顺序要求是**(经度, 纬度)**,而你的坐标是(纬度, 经度),需要反转才能正确生成地理图形。
适配圆形坐标的正确代码
以下是可以直接运行的代码,包含坐标格式转换和Shapefile生成:
import shapefile # 你的圆形经纬度坐标(纬度在前,经度在后) circle_coords = [(39.1852751477193, -4.661967433717266), (39.185270807935424, -4.6620560715526205), # 保留你提供的完整坐标列表即可 (39.1852751477193, -4.661967433717266)] # 转换坐标顺序为(经度, 纬度),符合Shapefile要求 converted_coords = [(lon, lat) for lat, lon in circle_coords] # 创建Shapefile Writer,指定多边形类型 with shapefile.Writer("circle_polygon", shapeType=shapefile.POLYGON) as w: # 添加字段 w.field("NAME", "C", 40) w.field("TYPE", "C", 40) # 添加多边形几何数据 w.polygon([converted_coords]) # 添加属性记录 w.record("Circle", "Polygon")
代码说明
- 坐标转换:遍历原始坐标,将每个元组的(纬度, 经度)反转成(经度, 纬度),这是Shapefile的标准地理坐标顺序。
- Writer初始化:使用
shapeType=shapefile.POLYGON明确指定生成多边形,同时用with语句自动处理文件关闭,避免资源泄漏。 - 添加几何数据:用
w.polygon()方法直接传入转换后的坐标列表,无需parts参数。 - 字段与记录:添加自定义属性字段,并写入对应的属性值。
运行代码后,会在当前目录生成三个文件:circle_polygon.shp、circle_polygon.shx、circle_polygon.dbf,这就是完整的多边形Shapefile。
内容的提问来源于stack exchange,提问作者bsh7194
相关产品推荐
相关产品推荐

