1

How can I load GeoDataFrame (populated from shapefile) to PostGIS when GEOM column contains MULTILINESTRING and LINESTRING e.g.:

LINESTRING (14.2384666 46.6304714, 14.2386703 46.6304123)

MULTILINESTRING ((13.90433020000000042 46.61830880000000121, 13.90393929999999934 46.61873270000000247, 13.90390771785250124 46.61876445408692859),(13.90327032848085764 46.61940531353186401, 13.90327032848085587 46.61940531353186401))

I have tried with "to_sql" but I need to specify GEOM type and when I chose

'geom':Geometry('MULTILINESTRING', srid=4326)}

I got an error: ProgrammingError: (psycopg2.ProgrammingError) can't adapt type 'LineString'

Vince
  • 20,017
  • 15
  • 45
  • 64
zwornik
  • 205
  • 1
  • 7

2 Answers2

0

You need to remove the MultiLinestring first.

GeoDataframe.explode()
GeoDataframe.to_sql()
ImanolUr
  • 1,102
  • 1
  • 10
  • 21
0

Based on answer from Casting geometry to MULTI using GeoPandas? I have found solution:

from shapely.geometry.linestring import LineString
from shapely.geometry.multilinestring import MultiLineString

geodataframe['geometry'] = [MultiLineString([feature]) if type(feature) == LineString else feature for feature in geodataframe['geometry']]
zwornik
  • 205
  • 1
  • 7