Guest User

Untitled

a guest
Mar 20th, 2018
74
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.16 KB | None | 0 0
  1. from shapely.geometry import shape, mapping
  2. import numpy as np
  3. # one polygon
  4. print poly.wkt
  5. POLYGON ((101.2200527190607 -114.6493019170767, 146.6225142079163 -114.4488495484725, 185.0301801801801 -114.2792792792793, 184.6581081081081 -217.7153153153153, 14.99324324324321 -218.4594594594595, 16.4815315315315 -115.0234234234234, 101.2200527190607 -114.6493019170767))
  6. # convert to GeoJSON format
  7. geojson = mapping(poly)
  8. print geojson
  9. {'type': 'Polygon', 'coordinates': (((101.2200527190607, -114.6493019170767), (146.6225142079163, -114.4488495484725), (185.0301801801801, -114.2792792792793), (184.6581081081081, -217.7153153153153), (14.99324324324321, -218.4594594594595), (16.4815315315315, -115.0234234234234), (101.2200527190607, -114.6493019170767)),)}
  10. geosjson['coordinates'] = np.round(np.array(geosjon['coordinates']),2)
  11. print geosjson
  12. {'type': 'Polygon', 'coordinates': array([[[ 101.22, -114.65],
  13. [ 146.62, -114.45],
  14. [ 185.03, -114.28],
  15. [ 184.66, -217.72],
  16. [ 14.99, -218.46],
  17. [ 16.48, -115.02],
  18. [ 101.22, -114.65]]])}
  19. print shape(geosjson)
  20. POLYGON ((101.22 -114.65, 146.62 -114.45, 185.03 -114.28, 184.66 -217.72, 14.99 -218.46, 16.48 -115.02, 101.22 -114.65))
  21.  
  22. def set_precision(coords, precision):
  23. result = []
  24. try:
  25. return round(coords, int(precision))
  26. except TypeError:
  27. for coord in coords:
  28. result.append(_set_precision(coord, precision))
  29. return result
  30. geojson = mapping(poly)
  31. geosjson['coordinates']= set_precision(geosjson['coordinates'], 2)
  32. print geosjson
  33. {'type': 'Polygon', 'coordinates': [[[101.22, -114.65], [146.62, -114.45], [185.03, -114.28], [184.66, -217.72], [14.99, -218.46], [16.48, -115.02], [101.22, -114.65]]]}
  34. print shape(geosjson)
  35. POLYGON ((101.22 -114.65, 146.62 -114.45, 185.03 -114.28, 184.66 -217.72, 14.99 -218.46, 16.48 -115.02, 101.22 -114.65))
  36.  
  37. geojson = {'type': 'Polygon', 'coordinates': [[[-73.92391, 41.31064], [-73.92391, 41.31069], [-73.92388, 41.31069], [-73.92388, 41.31064], [-73.92391, 41.31064]]]}
  38. print(str(shape(geojson)))
  39. POLYGON ((-73.92391000000001 41.31064, -73.92391000000001 41.31069, -73.92388 41.31069, -73.92388 41.31064, -73.92391000000001 41.31064))
Add Comment
Please, Sign In to add comment