Skip to content

Instantly share code, notes, and snippets.

@WindfallLabs
Last active November 10, 2015 23:59
Show Gist options
  • Save WindfallLabs/76ded0e96a0ba172adf2 to your computer and use it in GitHub Desktop.
Save WindfallLabs/76ded0e96a0ba172adf2 to your computer and use it in GitHub Desktop.
A proposed edition to geopandas.io for reading SpatiaLite data via wkt
"""Addition to geopandas.io.sql.py
by Garin Wally / Windfall Spatial, Nov 10, 2015
"""
# Place with the other imports
import shapely.wkt
def read_spatialite(sql, con, geom_col, crs=None, index_col=None,
coerce_float=True, params=None):
"""
Returns a GeoDataFrame corresponding to the result of the query
string, which must contain a geometry column.
Warning: the connection parameter must have extensions enabled,
and the 'mod_spatialite.dll' extension loaded.
Examples:
sql = "SELECT geom, kind FROM polygons;"
df = geopandas.read_spatialite(sql, con)
Parameters
----------
sql: string
con: DB connection object or SQLAlchemy engine
geom_col: string (removed default "geom")
column name to convert to shapely geometries
crs: optional
CRS to use for the returned GeoDataFrame
See the documentation for pandas.read_sql for further explanation
of the following parameters:
index_col, coerce_float, params
"""
# Read the full table as a DataFrame
df = read_sql(sql, con, index_col=index_col, coerce_float=coerce_float,
params=params)
# Ensure geometry column was specified correctly
if geom_col not in df:
raise ValueError("Geometry column specified does not exist: "
"'{0}'".format(geom_col))
# Ensure there are no Null geometries. I have bad data...
# prevents 'Windows Error: exception: access violation reading 0x0000000'
assert True not in set(df[geom_col].apply(lambda x: x is None)), \
("NULL geometries found! "
"Try adding 'WHERE {0} IS NOT NULL;'".format(geom_col))
# Collect just the geometry column as a separate DataFrame
from_sql = sql[sql.lower().index("from"):]
as_text = "ST_AsText({0})".format(geom_col)
geom_sql = "SELECT {0} {1}".format(as_text, from_sql)
wkt_geoms = read_sql(geom_sql, con)
# Convert to shapely geometries
shapely_geoms = wkt_geoms[as_text].apply(lambda x: shapely.wkt.loads(x.encode()))
# Combine the geometry and the rest of the data
df[geom_col] = GeoSeries(shapely_geoms)
return GeoDataFrame(df, crs=crs, geometry=geom_col)
@WindfallLabs
Copy link
Author

Working version.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment