from shapely.geometry import Point, LineString, Polygon
point1 = Point(2.2, 4.2)
point2 = Point(7.2, -25.1)
point3 = Point(9.26, -2.456)
point3D = Point(9.26, -2.456, 0.57)
type(point3D) # shapely.geometry.point.Point
print(point1) # POINT (2.2 4.2)
print(point3D) # POINT Z (9.26 -2.456 0.57), Z represents 3D point
point1
POINT (2.2 4.2) POINT Z (9.26 -2.456 0.57)
# get coordinates
point1.x, point1.y
point3D.x, point3D.y, point3D.z
(9.26, -2.456, 0.57)
# get distance
point1.distance(point2)
29.723559679150142
# create LineString
line = LineString([point1, point2, point3])
line2 = LineString([(2.2, 4.2), (7.2, -25.1), (9.26, -2.456)])
line
line_x = line.xy[0] # array('d', [2.2, 7.2, 9.26])
line_y = line.xy[1] # array('d', [4.2, -25.1, -2.456])
line.length
l_centroid = line.centroid # Point
poly = Polygon([point1, point2, point3])
poly2 = Polygon([(2.2, 4.2), (7.2, -25.1), (9.26, -2.456)])
poly
poly.exterior.coords.xy
(array('d', [2.2, 7.2, 9.26, 2.2]), array('d', [4.2, -25.1, -2.456, 4.2]))
poly.geom_type # Polygon
'Polygon'
world_exterior = [(-180, 90), (-180, -90), (180, -90), (180, 90)]
hole = [[(-170, 80), (-170, -80), (170, -80), (170, 80)]]
world = Polygon(shell=world_exterior) # without a hole
world
world_has_a_hole = Polygon(shell=world_exterior, holes=hole) # with a hole
world_has_a_hole
world.centroid # Point
world.area # 64800.0
world.bounds # (-180.0, -90.0, 180.0, 90.0)
world.exterior # LinearRing
world.exterior.length # 1080.0
1080.0
from shapely.geometry import MultiPoint, MultiLineString, MultiPolygon, box
multi_point = MultiPoint([point1, point2, point3])
multi_point2 = MultiPoint([(2.2, 4.2), (7.2, -25.1), (9.26, -2.456)])
multi_point
len(multi_point) # get the number of points
multi_point[0] # access point by index
line1 = LineString([point1, point2])
line2 = LineString([point2, point3])
multi_line = MultiLineString([line1, line2])
multi_line
len(multi_line) # number of LineString
multi_line[0] # first lineString
west_exterior = [(-180, 90), (-180, -90), (0, -90), (0, 90)]
west_hole = [[(-170, 80), (-170, -80), (-10, -80), (-10, 80)]]
west_poly = Polygon(shell=west_exterior, holes=west_hole)
min_x, min_y = 0, -90
max_x, max_y = 180, 90
east_poly_box = box(minx=min_x, miny=min_y, maxx=max_x, maxy=max_y)
multi_poly = MultiPolygon([west_poly, east_poly_box])
multi_poly
convex = multi_point.convex_hull # Polygon
multi_poly.area # 39200.0
39200.0
from shapely.geometry import mapping, Polygon
import fiona
# Here's an example Shapely geometry
poly = Polygon([(0, 0), (0, 1), (1, 1), (0, 0)])
poly2 = Polygon([(1, 1), (1, 2), (2, 2), (1, 1)])
schema = {
'geometry': 'Polygon',
'properties': {'id': 'int', 'type':'str'},
}
with fiona.open('my_shp2.shp', 'w', 'ESRI Shapefile', schema) as c:
## If there are multiple geometries, put the "for" loop here
c.write({
'geometry': mapping(poly),
'properties': {'id': 123, 'type': 'start'},
})
c.write({
'geometry': mapping(poly2),
'properties': {'id': 124, 'type': 'end'},
})
import geopandas as gpd
data = gpd.read_file('my_shp2.shp')
data
id | type | geometry | |
---|---|---|---|
0 | 123 | start | POLYGON ((0.00000 0.00000, 0.00000 1.00000, 1.... |
1 | 124 | end | POLYGON ((1.00000 1.00000, 1.00000 2.00000, 2.... |