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": " "
}
},
str = str.replace("(", "")
for both parentheses and then split by the comma. You should have your lists, and not get the iterable error – ytpillaistr.replace("[", "")
will result in all brackets being removed. Repeating this with the other bracket will leave me with as string of number with commas after each number, which is no good for distinguishing pairs.[[[-76.2671328, 38.4506304], [-76.2669856, 38.4505256], [-76.2668385, 38.4503701]]]
into-76.2671328, 38.4506304, -76.2669856, 38.4505256, -76.2668385, 38.4503701
but I guess I couldstr.replace("],", "|")
or another charter to distinguish pairs and then spitting on the same charter. – James Laney","
. THEN do the replacing. – ytpillai