Calculating bounding box a certain distance away from a lat/long coordinate in Java

后端 未结 6 2106
半阙折子戏
半阙折子戏 2020-11-28 23:47

Given a coordinate (lat, long), I am trying to calculate a square bounding box that is a given distance (e.g. 50km) away from the coordinate. So as input I have lat, long a

6条回答
  •  无人及你
    2020-11-29 00:21

    import com.vividsolutions.jts.geom.Envelope;
    
    ...
    Envelope env = new Envelope(centerPoint.getCoordinate());
    env.expandBy(distance_in_degrees); 
    ...
    

    Now env contains your envelope. It's not actually a "square" (whatever that means on the surface of a sphere), but it should do.

    You should note that the distance in degrees will depend on the latitude of the center point. At the equator, 1 degree of latitude is about 111km, but in New York, it's only about 75km.

    The really cool thing is that you can toss all your points into a com.vividsolutions.jts.index.strtree.STRtree and then use it to quickly calculate points inside that Envelope.

提交回复
热议问题