Guest User

Untitled

a guest
Oct 19th, 2018
92
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. point = ogr.Geometry(ogr.wkbPoint)
  2. # 2D point
  3. point.AddPoint(1198054.34, 648493.09)
  4. print point.ExportToWkt()
  5. POINT (1198054.34 648493.09 0)
  6. point.GetGeometryType()==ogr.wkbPoint
  7. False
  8. point.GetGeometryType()==ogr.wkbPoint25D
  9. True
  10.  
  11. from shapely.geometry import Point
  12. pt = Point(1198054.34, 648493.09)
  13. # conversion to an ogr geometry
  14. point = ogr.CreateGeometryFromWkb(pt.wkb)
  15. print(point.ExportToWkt())
  16. POINT (1198054.34 648493.09)
  17. point.GetGeometryType()==ogr.wkbPoint
  18. True
  19. point.GetGeometryType()==ogr.wkbPoint25D
  20. False
  21.  
  22. # The resulting WKT
  23. feature.ExportToWkt()
  24. 'Polygon ((250325.46051841106964275 142166.43077902274671942 0, 250775.29772301684715785 142019.67537893861299381 0, 250906.10144917882280424 141828.25529187230858952 0, 250555.1646228906174656 141550.69616562619921751 0, 250309.5088444888824597 141238.04335675123729743 0, 249933.04933992517180741 141777.20993532129796222 0, 250325.46051841106964275 142166.43077902274671942 0))'
  25.  
  26. json_geom = feature.ExportToJson()
  27. print json_geom
  28. '{ "type": "Polygon", "coordinates": [ [ [ 250325.460518411069643, 142166.430779022746719, 0.0 ], [ 250775.297723016847158, 142019.675378938612994, 0.0 ], [ 250906.101449178822804, 141828.25529187230859, 0.0 ], [ 250555.164622890617466, 141550.696165626199218, 0.0 ], [ 250309.50884448888246, 141238.043356751237297, 0.0 ], [ 249933.049339925171807, 141777.209935321297962, 0.0 ], [ 250325.460518411069643, 142166.430779022746719, 0.0 ] ] ] }'
  29. # convert json format to a Python dictionary
  30. import json
  31. geom = json.loads(json_geom)
  32. print geom['coordinates']
  33. [[[250325.4605184111, 142166.4307790227, 100.0], [250775.2977230168, 142019.6753789386, 150.0], [250906.1014491788, 141828.2552918723, 200.0], [250555.1646228906, 141550.6961656262, 230.0], [250309.5088444889, 141238.0433567512, 340.0], [249933.0493399252, 141777.2099353213, 220.0], [250325.4605184111, 142166.4307790227, 100.0]]]
  34. # slice the coordinates to eliminate 3D
  35. new_coords = [[i[:2] for i in geom['coordinates'][0]]]
  36. print new_coords
  37. [[[250325.4605184111, 142166.4307790227], [250775.2977230168, 142019.6753789386], [250906.1014491788, 141828.2552918723], [250555.1646228906, 141550.6961656262], [250309.5088444889, 141238.0433567512], [249933.0493399252, 141777.2099353213], [250325.4605184111, 142166.4307790227]]]
  38. new = { "type": "Polygon",'coordinates': new_coords}
  39. print new
  40. {'type': 'Polygon', 'coordinates': [[[250325.4605184111, 142166.4307790227], [250775.2977230168, 142019.6753789386], [250906.1014491788, 141828.2552918723], [250555.1646228906, 141550.6961656262], [250309.5088444889, 141238.0433567512], [249933.0493399252, 141777.2099353213], [250325.4605184111, 142166.4307790227]]]}
  41. # recreate the ogr geometry
  42. poly = ogr.CreateGeometryFromJson(json.dumps(new))
  43. poly.ExportToWkt()
  44. 'POLYGON ((250325.460518411098747 142166.430779022688512,250775.29772301678895 142019.675378938612994,250906.1014491787937 141828.25529187230859,250555.164622890588362 141550.696165626199218,250309.508844488911564 141238.043356751208194,249933.049339925200911 141777.209935321297962,250325.460518411098747 142166.430779022688512))'
  45.  
  46. from geomet import wkt
  47. wkt.dumps(new, decimals=4)
  48. 'POLYGON ((250325.4605 142166.4308, 250775.2977 142019.6754, 250906.1014 141828.2553, 250555.1646 141550.6962, 250309.5088 141238.0434, 249933.0493 141777.2099, 250325.4605 142166.4308))'
  49.  
  50. g = ogr.CreateGeometryFromWkt("POINT(1 1 1)")
  51. g.flattenTo2D()
  52. print(g.ExportToWkt())
  53. >> 'POINT (1 1)'
  54.  
  55. ring.AddPoint(coordinates['xmin'], coordinates['ymin'])
  56. ring.AddPoint(coordinates['xmin'], coordinates['ymax'])
  57. ring.AddPoint(coordinates['xmax'], coordinates['ymax'])
  58. ring.AddPoint(coordinates['xmax'], coordinates['ymin'])
  59. ring.CloseRings()
  60.  
  61. geom = ogr.Geometry(ogr.wkbPolygon)
  62. geom.AddGeometry(ring)
  63.  
  64. geom.FlattenTo2D()
RAW Paste Data Copied