5

I am a little confused about how objects are being defined and represented and mostly because some of the solutions I find for solving issues I have demand that the input data be in an other form than the one I manage to generate.

Let say that I wish to divide a LineString into 6 equal segments, then this method work perfectly:

from shapely.geometry import LineString, MultiPoint
from shapely.ops import split

line = LineString([(0, 0), (10, 10)]) splitter = MultiPoint([line.interpolate((i/4), normalized=True) for i in range(1, 4)]) split(line, splitter).wkt

which returns:

'GEOMETRYCOLLECTION (LINESTRING (0 0, 2.5 2.5), LINESTRING (2.5 2.5, 5 5), LINESTRING (5 5, 7.5 7.5), LINESTRING (7.5 7.5, 10 10))'

With my method (on my data)

zone_short_edges['line'] = zone_short_edges.apply(lambda row: LineString([row['fr_point'], row['to_point']]), axis=1) #Create a linestring column
zone_short_edges['midpoint'] = zone_short_edges.apply(lambda row: row['line'].centroid, axis=1) #Find centroid
zone_short_edges = zone_short_edges.set_geometry("line")
zone_short_edges = zone_short_edges.set_geometry("midpoint")

I generate POINTs and LINESTRINGs of this type:

         id  vertex_id                    fr_point  \
1  Allé 119          2  POINT (119.79008 28.35047)   
3  Allé 119          4  POINT (122.85067 44.85106)
                 to_point  seg_length  \

1 POINT (122.85067 28.08433) 3.072140
3 POINT (119.92314 44.71798) 2.930553

                                            line  \

1 LINESTRING (119.79008 28.35047, 122.85067 28.0...
3 LINESTRING (122.85067 44.85106, 119.92314 44.7...

                 midpoint  

1 POINT (121.32038 28.21740)
3 POINT (121.38690 44.78452)

that is, LINESTRING (119.79008 28.35047, 122.85067 28.0454, 122.3434 23.323). And I therefore cannot use the linestring splitting method. Also, the result of the method is linestrings in the form I have to begin with.

What is the difference between LineString([(0, 0), (10, 10)]) and LINESTRING((0 0), (10 10))?

Taras
  • 32,823
  • 4
  • 66
  • 137

1 Answers1

5

To simply answer your question, LINESTRING((0 0), (10 10)) (string) is the WKT representation of LineString([(0, 0), (10, 10)]) (geometry) used by shapely (and thus GeoPandas) to print a geometry.

from shapely.geometry import LineString

line = LineString([(0, 0), (10, 10)]) line <shapely.geometry.linestring.LineString object at 0x11e5bd438> print (line, type(line)) LINESTRING (0 0, 10 10) <class 'shapely.geometry.linestring.LineString'> wkt = line.wkt print(wkt,type(wkt)) LINESTRING (0 0, 10 10) <class 'str'>

From a WKT string to a shapely geometry:

from shapely.wkt import loads

line2 = loads(wkt) print(line2, type(line2)) LINESTRING (0 0, 10 10) <class 'shapely.geometry.linestring.LineString'>

With GeoPandas

import geopandas as gpd

df = gpd.read_file("lines.shp") df.geometry.iloc[0] <shapely.geometry.linestring.LineString object at 0x11e5bd630> print(df.geometry.iloc[0]) LINESTRING (-0.6551724137931035 -0.05491698595146888, -0.3358876117496808 0.4610472541507024, 0.07279693486590033 0.4763729246487867, 0.3690932311621966 0.3742017879948915, 0.5019157088122606 0.51213282247765)

Taras
  • 32,823
  • 4
  • 66
  • 137
gene
  • 54,868
  • 3
  • 110
  • 187