Example applications
Example: dissolve
Example: intersect
Shapely Examples to Supplement the Current Manual
Below are some additional examples to help supplement the current Shapely manual.
Examples for "2.7.4 Equals"
Points
>>> point_r.equals(Point(-1.5, 1.2)) True >>> point_r.equals(point_g) False
Line with reversed vertices is equal to itself:
>>> list(line_b.coords) [(-1.8, -1.2), (1.8, 0.5)] >>> list(reversed(line_b.coords)) [(1.8, 0.5), (-1.8, -1.2)] >>> line_b_rev = LineString(list(reversed(line_b.coords))) >>> line_b.equals(line_b_rev) True >>> line_b.equals(line_g) False
Likewise, a polygon with with the same vertices rotated in order is equal to itself:
Recall from "2.4.1 Example Geometries":
>>> polygon = Polygon(((-1.0, -1.0), (-1.0, 1.0), (1.0, 1.0), (1.0, -1.0)))
Move the last point to the front of coordinate list:
>>> polygon_rotated = Polygon(((1.0, -1.0), (-1.0, -1.0), (-1.0, 1.0), (1.0, 1.0))) >>> polygon.equals(polygon_rotated) True >>> polygon_b = Polygon(((2.0, -1.0), (-1.0, -1.0), (-1.0, 1.0), (2.0, 1.0))) >>> polygon.equals(polygon_b) False
Examples for "4.2 Python Sequences"
Multiple points can be handled with asMultiPoint
>>> from shapely.geometry import asMultiPoint >>> coords = [[3.0, 4.0], [1.0, -5.0], [9.0, 0.0]] >>> mpa = asMultiPoint(coords) >>> mpa.wkt 'MULTIPOINT (3.0000000000000000 4.0000000000000000, 1.0000000000000000 -5.000000 0000000000, 9.0000000000000000 0.0000000000000000)' >>> coords[0][0] = 2.0 >>> coords [[2.0, 4.0], [1.0, -5.0], [9.0, 0.0]] >>> mpa.wkt 'MULTIPOINT (2.0000000000000000 4.0000000000000000, 1.0000000000000000 -5.000000 0000000000, 9.0000000000000000 0.0000000000000000)'
Multiple lines can be handled with asMultiLineString
>>> from shapely.geometry import asMultiLineString >>> line1 = [[-72.0, 45.0], [-72.5, 47.7], [-73.0, 48.2]] >>> line2 = [[-82.0, 47.0], [-82.5, 55.1]] >>> coords = [line1, line2] >>> coords [[[-72.0, 45.0], [-72.5, 47.700000000000003], [-73.0, 48.200000000000003]], [[-8 2.0, 47.0], [-82.5, 55.100000000000001]]] >>> mla = asMultiLineString(coords) >>> mla.wkt 'MULTILINESTRING ((-72.0000000000000000 45.0000000000000000, -72.500000000000000 0 47.7000000000000030, -73.0000000000000000 48.2000000000000030), (-82.000000000 0000000 47.0000000000000000, -82.5000000000000000 55.1000000000000010))' >>> line1[0][0] = -72.5 >>> mla.wkt 'MULTILINESTRING ((-72.5000000000000000 45.0000000000000000, -72.500000000000000 0 47.7000000000000030, -73.0000000000000000 48.2000000000000030), (-82.000000000 0000000 47.0000000000000000, -82.5000000000000000 55.1000000000000010))'
Multiple polygons can be handled with asMultiPolygon
>>> from shapely.geometry import asMultiPolygon >>> poly1_coords = [[0.0, 0.0], [0.0, 1.0], [1.0, 1.0], [1.0, 0.0]] >>> poly1_hole_coords = [[[0.1,0.1], [0.1,0.2], [0.2,0.2], [0.2,0.1]]] >>> poly1 = [poly1_coords, poly1_hole_coords] >>> poly2_coords = [[5.0, 5.0], [0.0, 1.0], [1.0, 1.0], [1.0, 0.0]] >>> poly2_hole_coords = [] # no holes >>> poly2 = [poly2_coords, poly2_hole_coords] >>> coords = [poly1, poly2] >>> mpa = asMultiPolygon(coords) >>> mpa.wkt 'MULTIPOLYGON (((0.0000000000000000 0.0000000000000000, 0.0000000000000000 1.000 0000000000000, 1.0000000000000000 1.0000000000000000, 1.0000000000000000 0.00000 00000000000, 0.0000000000000000 0.0000000000000000), (0.1000000000000000 0.10000 00000000000, 0.1000000000000000 0.2000000000000000, 0.2000000000000000 0.2000000 000000000, 0.2000000000000000 0.1000000000000000, 0.1000000000000000 0.100000000 0000000)), ((5.0000000000000000 5.0000000000000000, 0.0000000000000000 1.0000000 000000000, 1.0000000000000000 1.0000000000000000, 1.0000000000000000 0.000000000 0000000, 5.0000000000000000 5.0000000000000000)))'
Examples for "4.3 Numpy Array Interface"
An array of multiple points can be loaded with asMultiPoint
>>> from numpy import array
>>> from shapely.geometry import asMultiPoint
>>> a = array([[1.0, 2.0], [7.7, 6.2], [-0.1, 3.9]])
>>> a
array([[ 1. , 2. ],
[ 7.7, 6.2],
[-0.1, 3.9]])
>>> a.shape
(3, 2)
>>> mpa = asMultiPoint(a)
>>> mpa.wkt
'MULTIPOINT (1.0000000000000000 2.0000000000000000, 7.7000000000000002 6.2000000
000000002, -0.1000000000000000 3.8999999999999999)'
>>> a[0]
array([ 1., 2.])
>>> a[0][1]
2.0
>>> a[0][1] = 3.5
>>> mpa.wkt
'MULTIPOINT (1.0000000000000000 3.5000000000000000, 7.7000000000000002 6.2000000
000000002, -0.1000000000000000 3.8999999999999999)'
Attachments
- dissolve.png (41.7 KB) - added by seang 7 months ago.
-
intersect.png
(39.1 KB) - added by seang
7 months ago.
Illustrate results of the intersect example


