Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Bring SRID in from_postgis() #451

Closed
adamboche opened this issue Jun 18, 2017 · 4 comments
Closed

Bring SRID in from_postgis() #451

adamboche opened this issue Jun 18, 2017 · 4 comments

Comments

@adamboche
Copy link

adamboche commented Jun 18, 2017

I have a geometry in postgis and query it with df = gpd.GeoDataFrame.from_postgis(). I then want to use df.to_crs(), but df doesn't seem to have a crs already, so it says ValueError: Cannot transform naive geometries. Please set a crs on the object first.

I can set df.crs = ..., but that seems fragile. Is it possible to make geopandas get the srid from postgis when it makes the query, and automatically set the crs?

@kuanb
Copy link

kuanb commented Jul 19, 2017

I'd like to second this. In my workflows, I tend to have a parse_wkt function that performs the following step:

    srid = None
    if geo_data.startswith('SRID'):
        semi_idx = geo_data.index(';')
        srid = int(geo_data[5:semi_idx])
        geo_data = geo_data[semi_idx + 1:]

    geo = shapely.wkt.loads(geo_data)

The srid is then held and passed along downstream to create a crs dictionary. Seeing as this logic is repeated consistently, and a common use case is to manipulate spatial query results from PG in memory, this would be a useful addition.

@emiliom
Copy link
Contributor

emiliom commented Aug 3, 2017

Yeah, I ran into this last November, with the current release.

Note that you can be pre-emptive and pass the crs as an argument to the from_postgis call:

seas = GeoDataFrame.from_postgis("select * from world_seas", conn, 
                                 geom_col='geometry', crs={'init': 'epsg:4326'}, 
                                 coerce_float=False)

But that's also fragile.

It would also be nice if geopandas could guess the geom_col; if only one geometry-type column is pulled by the query, it should be unambiguous.

I may be able to look into this and provide a PR over the next month or so, specially for the crs/SRID part, and specially if I can get some pointers from the developers. So far I see the relevant code (read_postgis) is at https://github.com/geopandas/geopandas/blob/master/geopandas/io/sql.py#L9, and to a lesser extent at https://github.com/geopandas/geopandas/blob/master/geopandas/geodataframe.py#L195 (the from_postgis warapper to read_postgis).

@emiliom
Copy link
Contributor

emiliom commented Aug 3, 2017

This need was noted as far back as August 2014, in #161; linking here to that issue b/c there are additional details and discussions there that may be helpful when trying to add this capability.

@jdmcbr
Copy link
Member

jdmcbr commented May 18, 2018

Closed by #627.

@jdmcbr jdmcbr closed this as completed May 18, 2018
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

5 participants