1

Given a a point (lat, lng) and a projection crs create a bounding box square given by miles to select rows in a geopandas GeoDataFrame where the geometry is made up of other points.

Seems like pyproject should do it but I can only find examples of converting from one projection to another. I want raw coordinates projected to the current projection. In this case miles would be nice.

import pandas as pd
import geopandas as gpd
import shapely as shp
#import pyproject ?

def rowsWithinDistanceByPoint(gdf, lat, lng, miles=10, crs='epsg:4269')

    # we do not know to use miles yet so ...
    tenmilesish = 1/6.0 #lame
    circle = shp.geometry.Point(lng, lat).buffer(tenmileish)
    # we really want converted coords
    #(xmin, ymin, xmax, ymax) = magicFunction(miles, crs)
    # but this sort of gets us there if were not next to the poles
    xmin, ymin, xmax, ymax = circle.bounds

    return gdf.cx[xmin:xmax, ymin:ymax]

So this should return the rows that are within 10 miles of the point.

user17130
  • 241
  • 1
  • 6

1 Answers1

0

So after hours of searching I found this to be relevant:

Get lat/long given current point, distance and bearing

I cannot believe this took half a day to solve! The red hearing was thinking that the projection mattered. That is taken care of when it was put into a geopandas dataframe.


import geopy
#from geopy.distance import VincentyDistance #changed names !!!!!
from geopy.distance import geodesic

def magicFunction(lat, lng, miles):
    # sw, ne
    bearings = [225, 45]
    origin = geopy.Point(lat, lng)
    l = []

    for bearing in bearings:
        destination = geodesic(miles=miles).destination(origin, bearing)
        coords = destination.longitude, destination.latitude
        l.extend(coords)
    # xmin, ymin, xmax, ymax
    return l

Using those coords to make a shapely.box and then plugging it into my existing code:

working code

Where the blue box is this code and the red oval is what I used in the question.

user17130
  • 241
  • 1
  • 6