| 7 | |
| 8 | |
| 9 | class LineString(LinearGeometryMixin, GEOSGeometry): |
| 10 | _init_func = capi.create_linestring |
| 11 | _minlength = 2 |
| 12 | has_cs = True |
| 13 | |
| 14 | def __init__(self, *args, **kwargs): |
| 15 | """ |
| 16 | Initialize on the given sequence: may take lists, tuples, NumPy arrays |
| 17 | of X,Y pairs, or Point objects. If Point objects are used, ownership is |
| 18 | _not_ transferred to the LineString object. |
| 19 | |
| 20 | Examples: |
| 21 | ls = LineString((1, 1), (2, 2)) |
| 22 | ls = LineString([(1, 1), (2, 2)]) |
| 23 | ls = LineString(array([(1, 1), (2, 2)])) |
| 24 | ls = LineString(Point(1, 1), Point(2, 2)) |
| 25 | """ |
| 26 | # If only one argument provided, set the coords array appropriately |
| 27 | if len(args) == 1: |
| 28 | coords = args[0] |
| 29 | else: |
| 30 | coords = args |
| 31 | |
| 32 | if not ( |
| 33 | isinstance(coords, (tuple, list)) |
| 34 | or numpy |
| 35 | and isinstance(coords, numpy.ndarray) |
| 36 | ): |
| 37 | raise TypeError("Invalid initialization input for LineStrings.") |
| 38 | |
| 39 | # If SRID was passed in with the keyword arguments |
| 40 | srid = kwargs.get("srid") |
| 41 | |
| 42 | ncoords = len(coords) |
| 43 | if not ncoords: |
| 44 | super().__init__(self._init_func(None), srid=srid) |
| 45 | return |
| 46 | |
| 47 | if ncoords < self._minlength: |
| 48 | raise ValueError( |
| 49 | "%s requires at least %d points, got %s." |
| 50 | % ( |
| 51 | self.__class__.__name__, |
| 52 | self._minlength, |
| 53 | ncoords, |
| 54 | ) |
| 55 | ) |
| 56 | |
| 57 | numpy_coords = not isinstance(coords, (tuple, list)) |
| 58 | if numpy_coords: |
| 59 | shape = coords.shape # Using numpy's shape. |
| 60 | if len(shape) != 2: |
| 61 | raise TypeError("Too many dimensions.") |
| 62 | self._checkdim(shape[1]) |
| 63 | ndim = shape[1] |
| 64 | else: |
| 65 | # Getting the number of coords and the number of dimensions, which |
| 66 | # must stay the same, e.g., no LineString((1, 2), (1, 2, 3)). |
no outgoing calls