从字符串

时间:2015-06-30 22:05:10

标签: python list parsing coordinates geojson

你好我整天都在这,但遇到了问题。

我正在阅读GeoJson文件并尝试提取坐标,以便我可以稍后遍历它们以确定我的窗口大小以重绘多边形,由坐标列表示。 每个多边形可以具有4-x个坐标对。 当我读到GeoJson时,我得到的列表只包含每个多边形的1个元素,而不是带有坐标列表的列表。我相信这是因为列表上写着:[[[-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]...]] 并且有多组[[]]使列表认为里面有一个元素..?

然后我尝试将坐标列表转换为字符串,这样我就可以删除额外的[[]]并提取坐标对并将它们转回列表中。这是代码:

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

来自这个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()

如果它的任何帮助是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": "                                                                                "
      }
    }, 

2 个答案:

答案 0 :(得分:1)

我不能完全确定您的代码,但我认为您的问题是,当您获得坐标的字符串值时(在polygons.append(feature['geometry']['coordinates'])coordinates.append(re.findall('[(.+?)]', newCords))中,您不会'39 ; t首先将它们转换为列表。

我们说我们有一个字符串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(","))

答案 1 :(得分:0)

在阅读了更多关于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