1

I'm trying to create a df through a GeoJSON url, merge it with a df created out of a SQL query, and create a shapefile/GeoJSON from the output. The output has everything put the .prj file (in the instance of a shapefile output). I've tried to set the CRS later in the code but it doesn't stay. I've discovered at some point in the merge, it drops the CRS.

import geopandas as gpd
import pyodbc
import pandas as pd
import numpy as np
import matplotlib.pyplot as plt

pd.set_option('display.max_rows', 500)
pd.set_option('display.max_columns', 500)
pd.set_option('display.width', 1000)

url = 'http is private just for this example.geojson'
df = gpd.read_file(url)
df = df.rename(columns={'MAINADDRESS': 'Address'})

cnxn = pyodbc.connect( xxxx)

sql = sql query
sql1 = 2nd sql query

df1 = pd.read_sql(sql, cnxn)
df1a = pd.read_sql(sql1, cnxn)

df4b = pd.merge(df1a, df1, on='strap')
df4c = df.merge(df4b, on='Address', how='left')
gdf = gpd.GeoDataFrame(df4c, geometry='geometry')
print(gdf.crs)
gdf.to_file('rentals.shp')
gdf.to_file('rentals.geojson', driver='GeoJSON')

The print(gdf.crs) shows 'None'.

  • I guess that you must set it with the code. Dropping the original CRS may make sense because your SQL can well be select ST_Tranform(geometry,xxxx). – user30184 Dec 12 '19 at 17:59

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.