I am attempting to take multipolygon data from a geojson, read it into a dataframe, and then create a table in mysql containing the data.
What's weird to me is that checking the dtype
at the end of the script will show the geometry column correctly as geometry
. However, checking the mysql db this column is showing as text
. Trying to convert the column to geometry or multipolygon type raises the error
1416 - Cannot get geometry object from data you send to the GEOMETRY field
I tried the following query, which may be where my issue is?
ALTER TABLE [table]
MODIFY COLUMN [column] GEOMETRY
Similar questions gave an answer to convert the data to WKT or WKB. However, using the to_wkb (or to_wkt) method and then running a query will result in the previously mentioned error as well. I also tried just making my own function with no luck. Python code below.
import geopandas
from geoalchemy2 import Geometry
from sqlalchemy import create_engine, types
df = geopandas.read_file('geodata.geojson')
# geodataframe = df.to_wkb()
hostname="localhost"
dbname="mydbname"
uname="iamroot"
pwd = "madeyoulook"
engine = create_engine(f'''mysql://{uname}:{pwd}@{hostname}/{dbname}''')
df.to_sql('geodatacounty', engine, if_exists='replace', index=False, dtype={'shape_leng': types.FLOAT , 'shape_area': types.FLOAT, '`geometry`': Geometry(geometry_type='MULTIPOLYGON', srid=4326)})