Python 从字符串中提取坐标列表
你好,我一整天都在做这件事,但遇到了一个问题 我正在读取GeoJson文件,并试图提取坐标,以便稍后可以遍历它们,以确定窗口的大小,从而重新绘制多边形,这些多边形由坐标列表表示。 每个多边形可以有4-x坐标对。 当我读入GeoJson时,我得到一个列表,每个多边形只包含一个元素,而不是一个包含坐标列表的列表。我相信这是因为清单上写着:Python 从字符串中提取坐标列表,python,list,parsing,coordinates,geojson,Python,List,Parsing,Coordinates,Geojson,你好,我一整天都在做这件事,但遇到了一个问题 我正在读取GeoJson文件,并试图提取坐标,以便稍后可以遍历它们,以确定窗口的大小,从而重新绘制多边形,这些多边形由坐标列表表示。 每个多边形可以有4-x坐标对。 当我读入GeoJson时,我得到一个列表,每个多边形只包含一个元素,而不是一个包含坐标列表的列表。我相信这是因为清单上写着:[[[-76.2671328,38.4506304],-76.2669856,38.4505256],-76.2668385,38.4503701],-76.266
[[[-76.2671328,38.4506304],-76.2669856,38.4505256],-76.2668385,38.4503701],-76.2667281,38.4502182],-76.2664568,38.4499759],-76.2661993,38.4497843],-76.2660108,38.4497192],[/code>
而且有多组[[]]使列表认为其中有一个元素
因此,我尝试将坐标列表转换为字符串,以便删除多余的[[]],提取坐标对,并将它们转换回列表。代码如下:
import json
import re
with open('landareas.json') as f:
landareas = json.load(f)
coordinates = []
polygons = []
for feature in landareas['features']:
polygons.append(feature['geometry']['coordinates'])
print len(polygons)
for polygon in polygons:
#print type(polygon)
#print len(polygon)
string = str(polygon)
newCords = string[2:len(string)-1]
print newCords
coordinates.append(re.findall('[(.+?)]', newCords))
for coordinate in coordinates:
print coordinate
print
所以现在当我这么做的时候,我得到的回报是:
['.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.']
去掉所有数字..:(
如果有人能帮我找到一种方法,将坐标精确到每个多边形的坐标列表中,这样我就可以在一个多边形列表中循环,每个多边形都包含一个坐标列表,那将非常感谢
我最初的错误是:
File "polygon_LatLong_xy.py", line 33, in <module>
for x,y in polygons[CORDINATES]:
TypeError: 'int' object is not utterable
文件“polygon\u LatLong\u xy.py”,第33行,在
对于多边形[CORDINATES]中的x,y:
TypeError:“int”对象不可发出
从这个python脚本:
import turtle as t
import json
RCID = 0
CORDINATES= 1
with open('landareas.json') as f:
landareas = json.load(f)
polygons = []
for feature in landareas['features']:
polygons.append([feature['properties']['RCID'], feature['geometry']['coordinates'] ])
map_width = 400
map_height = 400
minx = 180
maxx = -180
miny = 90
maxy = -90
for x,y in polygons[CORDINATES]:
if x < minx: minx = x
elif x > maxx: maxx = x
if y < miny: miny = y
elif y > maxy: maxy = y
dist_x = maxx - minx
dist_y = maxy - miny
x_ratio = map_width / dist_x
y_ratio = map_height / dist_y
def convert(point):
lon = point[0]
lat = point[1]
x = map_width - ((maxx - lon) * x_ratio)
y = map_height - ((maxy - lat) * y_ratio)
#Python turtle graphics start in the middle of the screen
#so we must offset the points so the are centered
x = x - (map_width/2)
y = y -(map_height/2)
return [x,y]
t.up()
first_pixel = None
for point in polygons[CORDINATES]:
pixel = convert(point)
if not first_pixel:
first_pixel = pixel
t.goto(pixel)
t.down()
t.goto(first_pixel)
t.write(str(polygons[RCID]), align="center", font=("Arial",16,"bold"))
t.up()
t.done()
将海龟作为t导入
导入json
RCID=0
科迪酸盐=1
将open('landareas.json')作为f:
landareas=json.load(f)
多边形=[]
对于陆地区域中的要素[“要素”]:
polygons.append([feature['properties']['RCID'],feature['geometry']['coordinates']))
地图宽度=400
地图高度=400
minx=180
maxx=-180
米尼=90
maxy=-90
对于多边形[CORDINATES]中的x,y:
如果xmaxx:maxx=x
如果ymaxy:maxy=y
dist_x=最大值-最小值
dist_y=maxy-miny
x_比率=地图宽度/距离x
y\u比率=地图高度/距离y
def转换(点):
lon=点[0]
纬度=点[1]
x=贴图宽度-((最大x-lon)*x\u比率)
y=地图高度-((最大-横向)*y\U比率)
蟒蛇图形开始在屏幕中部
#所以我们必须偏移这些点,使它们居中
x=x-(地图宽度/2)
y=y-(地图高度/2)
返回[x,y]
t、 up()
第一像素=无
对于多边形中的点[Cordinate]:
像素=转换(点)
如果不是第一个像素:
第一像素=像素
t、 转到(像素)
t、 向下()
t、 转到(第一个像素)
t、 书写(str(多边形[RCID]),align=“center”,font=(“Arial”,16,“粗体”))
t、 up()
t、 完成()
如果此处的帮助是geoJson文件的摘录:
{
"type": "FeatureCollection",
"features": [
{
"geometry": {
"type": "Polygon",
"coordinates": [ <--- note the
[ <----------------two sets of brackets? could this be why?
[
-76.0220181,
38.1321203
],
[
-76.0219133,
38.1321847
],
[
-76.0232178,
38.1312463
],
[
-76.0230198,
38.1312923
],
[
-76.0220181,
38.1321203
]
]
]
},
"type": "Feature",
"properties": {
"TXTDSC": " ",
"RCID": 3918,
"PRIM": 3,
"NINFOM": " ",
"SORIND": " ",
"RECDAT": " ",
"AGEN": 550,
"GRUP": 1,
"SORDAT": " ",
"OBJL": 71,
"NOBJNM": " ",
"INFORM": " ",
"LNAM": "0226088C104B1046",
"STATUS": " ",
"RECIND": " ",
"SCAMAX": null,
"NTXTDS": " ",
"CONDTN": null,
"FIDS": 4166,
"SCAMIN": null,
"FIDN": 143396939,
"RVER": 1,
"OBJNAM": " "
}
},
{
“类型”:“FeatureCollection”,
“特点”:[
{
“几何学”:{
“类型”:“多边形”,
“坐标”:[我不能完全确定您的代码,但我认为您的问题是,当您获得坐标的字符串值时(在polygons.append(feature['geometry']['coordinates'])
和coordinates.append(re.findall('[(.+)]],newCords))
,您不会首先将它们转换为列表
假设我们有一个字符串c=“1,2”
您可以执行biglist.append(c.split(“,”)
还有一件事可能会有所帮助,那就是为每个多边形创建一组坐标
#Make a condition that tells the program when to search for the next polygon
polygons = []
polygon = []
for x in landareas['features']:
if polygonDone:
polygons.append(polygon)
del polygon[:]
else:
polygon.append(x['geometry']['coordinates'].split(","))
在阅读了关于re的更多内容后,我能够找到这段代码,找到x,y对,并将它们转换回x,y对的列表,尽管它们仍然表示为字符串
for feature in landareas['features']:
string_listof_cords = str(feature['geometry']['coordinates'])
#print string_cords
string_listof_cords = string_listof_cords.replace(",", "")
print(re.findall(r'\D\d*\.\d*\s\d*\.\d*', string_listof_cords))
然而,在我的搜索中,我找到了一种更好的方法来完成我最初尝试做的事情,从形状文件中操纵多边形,并将这种方法一起放弃。感谢@ytpillai的帮助,您还应该了解系统为什么会想到多边形[CORDINATES]是一个整数而不是数组。我对我的答案中的错误做出了一个理论,但这可以解释很多事情:).我同意,这就是我如何将坐标对列表转换为坐标对haOK的字符串的原因,因此在您创建单独的坐标对字符串之后。通过str=str.replace(“,”)取出括号
用两个括号括起来,然后用逗号分开。你应该有你的列表,而不是用str.replace(“[”,”)删除括号
将导致删除所有括号。用另一个括号重复此操作将使我在每个数字后留下一个带逗号的数字字符串,这对区分对是不利的。[[-76.2671328,38.4506304],-76.2669856,38.4505256],-76.2668385,38.4503701]]
进入-76.2671328,38.4506304,-76.2669856,38.4505256,-76.2668385,38.4503701
但我想我可以str.replace(“],”,“|”)
或另一个章程来区分对,然后在同一个章程上吐痰。啊,你的问题是你把所有的东西都放在一个字符串中。好的,这就是你要做的。首先,你需要通过将字符串拆分为“”,“
。然后进行替换。当我调用功能['geometry']['coordinates']
它返回一个类型为list的on对象。每个值都是字符串吗?如果不是,你可能需要找到一种方法,只迭代你想要的类型。坐标不是字符串。我只是将它们转换成一个,这样我就可以操作现在的坐标字符串。你是单独制作坐标字符串还是制作一个大字符串?没问题,很高兴您找到了一个解决方案:)