[GIS] Generate polygon automatically based on locations

google mapsopen-source-routing-machineopenstreetmap

I'm trying to achieve some similar to Draw a polygon automatically through markers (points) in google maps but not exactly.

Given a set of locations I'd like to know if it is possible to generate the polygon container for these locations automatically. Something like what Google does with fitBounds method. But if possible, I'd like the bounds snapped to streets. For example:

Given the current set of locations:

lat: 41.87368, lon: -87.65606
lat: 41.87388, lon: -87.65452
lat: 41.87327, lon: -87.65525
lat: 41.87241, lon: -87.65636
lat: 41.87317, lon: -87.64001
lat: 41.87397, lon: -87.64186
lat: 41.87397, lon: -87.64027

my desired polygons would be:

enter image description here

The one in red and also acceptable would be a variant with the one in green.

Link to map with locations: http://bl.ocks.org/anonymous/raw/f0e736dadb07a0634b228e9ff55fcbf9/

Is this possible? I'm doing this on my backend, so my question narrows it down to available APIs that could do it. I'm currently working with OSRM, Google Maps (regular and static APIs) and OpenStreetMap.

To summarize, given a set of locations are there any APIs that would automatically generate the bounds polygon for me?

Best Answer

To create a box snapped to streets, take a street layer, make the polylines into polygons. Intersect your points with those polygons; merge those blocks together. If the blocks don't connect, create centroids for your two polygons. Create a line between centroids; use that to select additional blocks. Repeat until you have a single polygon. The edges of that polygon should match streets.