Reputation: 23
Hello Iv been at this all day but have ran into a problem.
I am reading in GeoJson file and trying to extract the coordinates so that I can later iterate throughout them to determine the size of my window to redraw the polygons, represented by a list of coordinates.
Each polygon may have 4 - x coordinate pairs.
When I read in the GeoJson I get a list containing only 1 element for each polygon instead of a list with a list of coordinates. I believe this is because the list reads: [[[-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]...]]
and there are multiple sets of [[]] which make the list think there is one element inside..?
So then i tried converting the list of coordinate to a string so I could remove the extra [[]] and extract the coordinate pairs and turn them back into a list. Here is the 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
So now when i do this all i get back is:
['.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.']
With all the numbers stripped out.. :(!
If someone could please help me out in a method to exact the coordinates into a list of coordinates for each polygon so that I can loop through a list of polygons with each containing a list of their coordinates, that would be greatly apprcieated!
My initial error which started this was:
File "polygon_LatLong_xy.py", line 33, in <module>
for x,y in polygons[CORDINATES]:
TypeError: 'int' object is not utterable
from this python script:
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()
If its any help here is an excerpt from the geoJson file:
{
"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": " "
}
},
Upvotes: 2
Views: 3767
Reputation: 23
After reading more about re i was able to come up with this bit of code that found the x,y pairs and turn them back into a list of x,y pairs, though they are still represented as a string.
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))
However, in my searching I found a better way to do what I was initially trying todo, manipulate polygons from shape files, and have such aborted this method all together. Thanks for the help @ytpillai
Upvotes: 0
Reputation: 3550
I'm not complete sure about your code, but I think your problem is that when you get the string value of the coordinates (in the polygons.append(feature['geometry']['coordinates'])
and coordinates.append(re.findall('[(.+?)]', newCords))
, you don't convert them into a list first.
Let's say we have a string c = "1, 2"
You can do biglist.append(c.split(","))
One more thing that might help is that you make a set of coordinates for each polygon.
#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(","))
Upvotes: 1