I'm trying to add a GeoJSON file into the SQL Server database. I have this following script that works fine for inputting the geospatial data into MYSQL Database, thus creating a GEOMETRY type column in the DB.
import geopandas as gpd
import pymysql
import pymssql
from sqlalchemy import *
from sqlalchemy import func
from sqlalchemy.types import UserDefinedType
class Geometry(UserDefinedType):
def get_col_spec(self):
return "GEOMETRY"
def bind_expression(self, bindvalue):
return func.ST_GeomFromText(bindvalue, type_=self)
def column_expression(self, col):
return func.ST_AsText(col, type_=self)
geodataframe = gpd.read_file(r"my_geojson_file.geojson")
DATABASE_URL_SQLSERVER = 'mssql+pymssql://username:password@<server>.database.windows.net/database?charset=utf8'
DATABASE_URL_MYSQL = 'mysql+pymysql://username:password@server/database?charset=utf8'
engine = create_engine(DATABASE_URL_MYSQL)
geodataframe.to_sql('my_new_geom_table', engine, if_exists='replace', index=False,dtype={'geometry':Geometry})
However, when I follow this same structure to add it to sqlserver
database (I used the variable DATABASE_URL_SQLSERVER
instead of DATABASE_URL_MYSQL
[see code above] containing the string connection), It gives me the following error:
File "C:\Users\matheus.barros\Anaconda3\envs\geo_env\lib\site-packages\sqlalchemy\engine\default.py", line 689, in do_executemany
cursor.executemany(statement, parameters)
File "src\pymssql.pyx", line 476, in pymssql.Cursor.executemany
File "src\pymssql.pyx", line 450, in pymssql.Cursor.execute
File "src\_mssql.pyx", line 1070, in _mssql.MSSQLConnection.execute_query
File "src\_mssql.pyx", line 1101, in _mssql.MSSQLConnection.execute_query
File "src\_mssql.pyx", line 1218, in _mssql.MSSQLConnection.format_and_run_query
File "src\_mssql.pyx", line 1240, in _mssql.MSSQLConnection.format_sql_command
File "src\_mssql.pyx", line 1879, in _mssql._substitute_params
File "src\_mssql.pyx", line 1856, in _mssql._quote_data
File "src\_mssql.pyx", line 1832, in _mssql._quote_or_flatten
ValueError: expected a simple type, a tuple or a list
Someone has a hint in how to overcome that?