0

I have an array of Polygons where each object looks like this

{'type': 'Polygon', 'coordinates': (((-98.03706782884247, 44.72279408402323), (-98.03706782884247, 44.71524098343729), (-98.02951472825653, 44.71524098343729), (-98.02951472825653, 44.72279408402323), (-98.03706782884247, 44.72279408402323)),)}

I need to convert the coordinates to an array and it should look like

[[[-98.03706782884247, 44.72279408402323],[-98.03706782884247, 44.71524098343729], [-98.02951472825653, 44.71524098343729], [-98.02951472825653, 44.72279408402323], [-98.03706782884247, 44.72279408402323]]]

Is there a pre-defined method which is available for polygons?

2 Answers 2

1

Since this is tagged shapely, have a look at shapely.geometry.asShape() to interpret a GeoJSON-like mapping as a shapely object

import numpy as np
from shapely.geometry import asShape

d = {'type': 'Polygon', 'coordinates': (((-98.03706782884247, 44.72279408402323), (-98.03706782884247, 44.71524098343729), (-98.02951472825653, 44.71524098343729), (-98.02951472825653, 44.72279408402323), (-98.03706782884247, 44.72279408402323)),)}

geom = asShape(d)

print(geom)

# LinearRings can be viewed as numpy arrays
print(np.asarray(geom.exterior))
for interior in geom.interiors:
    print(np.asarray(interior))
0

You could convert the coordinates key values to a list.

what_your_looking_for = list(your_json_object['coordinates'])

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.