Skip to content
Advertisement

Import mssql spatial fields into geopandas/shapely geometry

I cannot seem to be able to directly import mssql spatial fields into geopandas. I can import normal mssql tables into pandas with Pymssql without problems, but I cannot figure out a way to import the spatial fields into shapely geometry. I know that the OGR driver for mssql should be able to handle it, but I’m not skilled enough in sql to figure this out. This is more of an issue for lines and polygons as points can be converted to x and y coordinates from the mssql field. Thanks!

Advertisement

Answer

I figured it out by properly querying the sql database table and converting the wkt string to shapely geometry via the loads function in shapely.wkt.

I’m no programmer, so bear that in mind with the organization of the function. The function can import mssql tables with or without GIS geometry.

JavaScript

EDIT: Made the function automatically find the geometry field if one exists.

Advertisement