Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- point = ogr.Geometry(ogr.wkbPoint)
- # 2D point
- point.AddPoint(1198054.34, 648493.09)
- print point.ExportToWkt()
- POINT (1198054.34 648493.09 0)
- point.GetGeometryType()==ogr.wkbPoint
- False
- point.GetGeometryType()==ogr.wkbPoint25D
- True
- from shapely.geometry import Point
- pt = Point(1198054.34, 648493.09)
- # conversion to an ogr geometry
- point = ogr.CreateGeometryFromWkb(pt.wkb)
- print(point.ExportToWkt())
- POINT (1198054.34 648493.09)
- point.GetGeometryType()==ogr.wkbPoint
- True
- point.GetGeometryType()==ogr.wkbPoint25D
- False
- # The resulting WKT
- feature.ExportToWkt()
- '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))'
- json_geom = feature.ExportToJson()
- print json_geom
- '{ "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 ] ] ] }'
- # convert json format to a Python dictionary
- import json
- geom = json.loads(json_geom)
- print geom['coordinates']
- [[[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]]]
- # slice the coordinates to eliminate 3D
- new_coords = [[i[:2] for i in geom['coordinates'][0]]]
- print new_coords
- [[[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]]]
- new = { "type": "Polygon",'coordinates': new_coords}
- print new
- {'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]]]}
- # recreate the ogr geometry
- poly = ogr.CreateGeometryFromJson(json.dumps(new))
- poly.ExportToWkt()
- '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))'
- from geomet import wkt
- wkt.dumps(new, decimals=4)
- '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))'
- g = ogr.CreateGeometryFromWkt("POINT(1 1 1)")
- g.flattenTo2D()
- print(g.ExportToWkt())
- >> 'POINT (1 1)'
- ring.AddPoint(coordinates['xmin'], coordinates['ymin'])
- ring.AddPoint(coordinates['xmin'], coordinates['ymax'])
- ring.AddPoint(coordinates['xmax'], coordinates['ymax'])
- ring.AddPoint(coordinates['xmax'], coordinates['ymin'])
- ring.CloseRings()
- geom = ogr.Geometry(ogr.wkbPolygon)
- geom.AddGeometry(ring)
- geom.FlattenTo2D()
Add Comment
Please, Sign In to add comment