Split a geometry by another geometry and return a collection of geometries. This function is the theoretical opposite of the union of the split geometry parts. If the splitter does not split the geometry, a collection with a single geometry equal to the input geometry is
(geom, splitter)
| 460 | |
| 461 | @staticmethod |
| 462 | def split(geom, splitter): |
| 463 | """Split a geometry by another geometry and return a collection of geometries. |
| 464 | |
| 465 | This function is the theoretical opposite of the union of |
| 466 | the split geometry parts. If the splitter does not split the geometry, a |
| 467 | collection with a single geometry equal to the input geometry is |
| 468 | returned. |
| 469 | |
| 470 | The function supports: |
| 471 | - Splitting a (Multi)LineString by a (Multi)Point or (Multi)LineString |
| 472 | or (Multi)Polygon |
| 473 | - Splitting a (Multi)Polygon by a LineString |
| 474 | |
| 475 | It may be convenient to snap the splitter with low tolerance to the |
| 476 | geometry. For example in the case of splitting a line by a point, the |
| 477 | point must be exactly on the line, for the line to be correctly split. |
| 478 | When splitting a line by a polygon, the boundary of the polygon is used |
| 479 | for the operation. When splitting a line by another line, a ValueError |
| 480 | is raised if the two overlap at some segment. |
| 481 | |
| 482 | Parameters |
| 483 | ---------- |
| 484 | geom : geometry |
| 485 | The geometry to be split |
| 486 | splitter : geometry |
| 487 | The geometry that will split the input geom |
| 488 | |
| 489 | Examples |
| 490 | -------- |
| 491 | >>> import shapely.ops |
| 492 | >>> from shapely import Point, LineString |
| 493 | >>> pt = Point((1, 1)) |
| 494 | >>> line = LineString([(0,0), (2,2)]) |
| 495 | >>> result = shapely.ops.split(line, pt) |
| 496 | >>> result.wkt |
| 497 | 'GEOMETRYCOLLECTION (LINESTRING (0 0, 1 1), LINESTRING (1 1, 2 2))' |
| 498 | |
| 499 | """ |
| 500 | if geom.geom_type in ("MultiLineString", "MultiPolygon"): |
| 501 | return GeometryCollection( |
| 502 | [i for part in geom.geoms for i in SplitOp.split(part, splitter).geoms] |
| 503 | ) |
| 504 | |
| 505 | elif geom.geom_type == "LineString": |
| 506 | if splitter.geom_type in ( |
| 507 | "LineString", |
| 508 | "MultiLineString", |
| 509 | "Polygon", |
| 510 | "MultiPolygon", |
| 511 | ): |
| 512 | split_func = SplitOp._split_line_with_line |
| 513 | elif splitter.geom_type == "Point": |
| 514 | split_func = SplitOp._split_line_with_point |
| 515 | elif splitter.geom_type == "MultiPoint": |
| 516 | split_func = SplitOp._split_line_with_multipoint |
| 517 | else: |
| 518 | raise GeometryTypeError( |
| 519 | f"Splitting a LineString with a {splitter.geom_type} is " |