0

I am trying to read a geospatial table from a PostGIS database on PostgreSQL. Using the funcion

sql = '''SELECT ST_AsText(geom) AS geom  FROM my_table;'''
df = gpd.GeoDataFrame.from_postgis(sql, con, geom_col='geom' )

I got back this error

ParseException: Invalid HEX char
Traceback (most recent call last):
  File "nuts3_grid_intersection.py", line 35, in <module>
    df = gpd.GeoDataFrame.from_postgis(sql, con, geom_col='geom' )
  File "/usr/local/lib/python3.6/dist-packages/geopandas/geodataframe.py", line 274, in from_postgis
    index_col=index_col, coerce_float=coerce_float, params=params)
  File "/usr/local/lib/python3.6/dist-packages/geopandas/io/sql.py", line 55, in read_postgis
    geoms = df[geom_col].apply(load_geom)
  File "/usr/local/lib/python3.6/dist-packages/pandas/core/series.py", line 3194, in apply
    mapped = lib.map_infer(values, f, convert=convert_dtype)
  File "pandas/_libs/src/inference.pyx", line 1472, in pandas._libs.lib.map_infer
  File "/usr/local/lib/python3.6/dist-packages/geopandas/io/sql.py", line 54, in load_geom
    return shapely.wkb.loads(str(x), hex=hex_encoded)
  File "/usr/local/lib/python3.6/dist-packages/shapely/wkb.py", line 14, in loads
    return reader.read_hex(data)
  File "/usr/local/lib/python3.6/dist-packages/shapely/geos.py", line 409, in read_hex
    "Could not create geometry because of errors "
shapely.errors.WKBReadingError: Could not create geometry because of errors while reading input.

I am not able to find a solution for it. I have geom data on a WKB format. I had the same issue also if I convert them using the functions ST_AsBinary(geom) and ST_GeomFromWKB("geom",SRID).

I run the PostgiGIS code:

SELECT ST_GeomFromWKB(decode('geom ', 'hex'), 4258) FROM my_table;

I got that output

ERROR:  invalid hexadecimal digit: "g"
********** Error **********

ERROR: invalid hexadecimal digit: "g"
SQL state: 22023

How can postgis read the geometry data even if the HEx standard on the WKB is not correct?

How could I solve this?

  • 1
    you are passing in 'geom', i.e. the column name as a string! use with ST_GeomFromWKB(geom, 4326) (no "") to get the geometry in PostGIS' universal hex format. (to make that clear: decode('geom', 'hex') will try to decode the string 'geom', and not the text representation of the geom column values) – ThingumaBob Dec 14 '18 at 13:35
  • yes, that's was because I created that table with pandas and I didn't realize that I had values on the table without geometries. When I wanted to make a geo-spatial operation, geopandas tolde me It was not able to read the geometries correctly (because some or them were None!!) I deleted it from the table and I had no issue... – Glori P. Dec 14 '18 at 14:31
  • Have you solved your issue? :) – kogexo Jan 10 at 16:54
  • @kogexo, yes. The issues was create a geospatial postgis table using pandas and not geopandas. I had no issue to visualize the data on QGIS but I did have problems when I tried to used geopandas. I solved the issue create the postgis table using geopandas :) – Glori P. Jan 14 at 10:54
  • @GloriP. Nice, glad to read that! My problem was that geometries were multiparts and not singleparts. Thanks for your answer. – kogexo Jan 15 at 14:26

Your Answer

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

Browse other questions tagged or ask your own question.