0

I have extracted linestring from Landsat images. now, I am literally stuck what to do? I want to convert it into shapefile. It is actually coastline geometry.

   **In[1]:** type(water_lines_smooth)
    **Out[1]:** list
    **In[2]:** print(water_lines_smooth)
    **Out[2]:**
    [<shapely.geometry.linestring.LineString at 0x2d11cd01c08>,
     <shapely.geometry.linestring.LineString at 0x2d11019f048>,
     <shapely.geometry.linestring.LineString at 0x2d11cd09d08>,
     <shapely.geometry.linestring.LineString at 0x2d11cceac08>,
     <shapely.geometry.linestring.LineString at 0x2d11ca7d0c8>,
     <shapely.geometry.linestring.LineString at 0x2d11ca7d108>,
     <shapely.geometry.linestring.LineString at 0x2d142747f88>,
     <shapely.geometry.linestring.LineString at 0x2d142747ec8>,
     <shapely.geometry.linestring.LineString at 0x2d142747088>,
     <shapely.geometry.linestring.LineString at 0x2d11ca7d148>,
     <shapely.geometry.linestring.LineString at 0x2d142747e48>,
     <shapely.geometry.linestring.LineString at 0x2d142747d08>,
     <shapely.geometry.linestring.LineString at 0x2d142747d48>,
     <shapely.geometry.linestring.LineString at 0x2d142747cc8>,
     <shapely.geometry.linestring.LineString at 0x2d142747408>,
     <shapely.geometry.linestring.LineString at 0x2d142747e08>,...

    **In[3]:** [{'geometry': x.__geo_interface__} for x in water_lines_smooth]
    **Out[3]:**
    {'geometry': {'type': 'LineString',
       'coordinates': ((377205.43543222273, 2464301.85113062),
        (377235.43946816603, 2464301.85113062),
        (377235.43946816603, 2464241.855100152),
        (377205.43543222273, 2464241.855100152),
    (375315.1811677954, 2464391.8451763224),
        (375285.1771318521, 2464331.8491458544),
        (375225.1690599655, 2464331.8491458544),
        (375165.1609880789, 2464361.8471610886),....
I have tried to convert it into geodataframe but there is geometry column issue.


     **In[4]:** df=gpd.GeoDataFrame(water_lines_smooth)
     I**n[5]:** df.to_file("shapef.shp") 
    AttributeError: No geometry data set yet (expected in column 'geometry'.) –
 

is there any alternative way to get shapefile?

1
  • yes, I have. I have tried to convert to geodataframe , it says, there is no geometry. I think, I need to work on how to do this. Aug 4 '20 at 15:37
0

Your code is incomplete so impossible to tell whats wrong. But this is working when I try:

import geopandas as gpd
from shapely.geometry import LineString
coords = [(377205.43543222273, 2464301.85113062),
        (377235.43946816603, 2464301.85113062),
        (377235.43946816603, 2464241.855100152),
        (377205.43543222273, 2464241.855100152),
        (375315.1811677954, 2464391.8451763224),
        (375285.1771318521, 2464331.8491458544),
        (375225.1690599655, 2464331.8491458544),
        (375165.1609880789, 2464361.8471610886)]

aline = LineString(coords)
bline = LineString([(c[0]+100, c[1]) for c in coords])
d = {'col1': ['name1','name2'], 'geometry': [aline, bline]}
df = gpd.GeoDataFrame(d, crs="EPSG:32618") #(change epsg)
df.to_file('/home/bera/GIS/Data/testdata/lines1234.shp')
0

The shapely dont export to shapefile. I think that the geopandas do that due the Fiona. So, I recommend you to create a GeoDataFrame and then just create the shapefile with the method geoDataFrame.to_file("<>.shp").

1
  • I am still getting error df=gpd.GeoDataFrame(water_lines_smooth) df.to_file("shapef.shp") AttributeError: No geometry data set yet (expected in column 'geometry'.) Aug 4 '20 at 15:42

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.