pysal / libpysal

Core components of Python Spatial Analysis Library
http://pysal.org/libpysal
Other
259 stars 78 forks source link

CRS checking utility #246

Open jGaboardi opened 4 years ago

jGaboardi commented 4 years ago

Following along pysal/tobler#50 and pysal/segregation#96 that @knaaptime raised regarding a new pyproj release, we may want to consider an embedded utility function that that checks for differing CRS of datasets.

jGaboardi commented 4 years ago

See also Joris' blog post.

knaaptime commented 4 years ago

well we got automatic utm zone detection in pyproj >=2.6, so i'd say the way to handle this would be to pin against that version.

then, we can test whether one CRS is equivalent to another and reproject into an appropriate crs if necessary. We'd rely solely on pyproj for that and no need to add extra handlers

knaaptime commented 4 years ago

looking a bit closer at what was added to pyproj, i need to revise this--all it does is check whether the crs is a utm projection.

so we still need something like

def guess_utm(gdf, reproject=True):

    # calculate the centroid of the union of all the geometries in the
    # GeoDataFrame
    avg_longitude = gdf["geometry"].unary_union.centroid.x

    # calculate the UTM zone from this avg longitude and define the UTM
    # CRS to project
    utm_zone = int(math.floor((avg_longitude + 180) / 6.0) + 1)
    utm_crs = "+proj=utm +zone={} +ellps=WGS84 +datum=WGS84 +units=m +no_defs".format(
        utm_zone
    )
    if reproject:
        # project the GeoDataFrame to the UTM CRS
        projected_gdf = gdf.to_crs(utm_crs)

        return projected_gdf
    return utm_crs

thats a pretty small and simple function now, so we could just repeat it across the packages, but could also make sense in libpysal

jGaboardi commented 4 years ago

And we could swap in native pysal geometries is that kind of check, correct?

knaaptime commented 4 years ago

🤷‍♂ I'm not sure, actually. i dont think ive ever used native pysal geometries. like, ever 😬

knaaptime commented 4 years ago

one small complication is that if we wanted this in libpysal, there's not really anywhere in the current namespace that's super appropriate

jGaboardi commented 4 years ago

one small complication is that if we wanted this in libpysal, there's not really anywhere in the current namespace that's super appropriate

What about common.py?

knaaptime commented 4 years ago

huh. i didnt realize that was exposed at the top level... thought it was jsut io, examples, cg and weights

yeah, common would be the place