Getting all zip codes within a radius of n miles

What is the best way to get a function like the following:

def getNearest(zipCode, miles): 

That is, given the zipcode (07024) and radius, return all zipcodes that are within this radius?

+4
location geography zipcode
source share
3 answers

SourceForge has a project that could help with this:

http://sourceforge.net/projects/zips/

It gives you a database with zip codes and their latitude / longitude, as well as coding examples of how to calculate the distance between two sets of coordinates. There is probably a better way to do this, but you can get your function, get the zipcode and its coordinates, and then go through each zipcode in the list and add the zipcode to the list if it falls within the specified number of miles.

+7
source share

If you want this to be accurate, you should start with the polygon data, which includes the location and shape of each zip code. I have a database similar to this one (usually a census published in the USA, but they don’t do this anymore) and built similar things on top of it, but not this exact request.

If you don't care about being accurate (which I guess you don’t know), you can get a table of center zipcodes and query points arranged in a large circle . PostGIS provides excellent tools for this, although you can query other databases that will perform similar tasks.

The alternative approach I used is to create a field that spans the circle you want by querying the inter clause on lon / lat and then making a big circle in the application code.

+3
source share

Maybe this can help. However, the project is configured in kilometers. You can change them in CityDAO.java

 public List<City> findCityInRange(GeoPoint geoPoint, double distance) { List<City> cities = new ArrayList<City>(); QueryBuilder queryBuilder = geoDistanceQuery("geoPoint") .point(geoPoint.getLat(), geoPoint.getLon()) //.distance(distance, DistanceUnit.KILOMETERS) original .distance(distance, DistanceUnit.MILES) .optimizeBbox("memory") .geoDistance(GeoDistance.ARC); SearchRequestBuilder builder = esClient.getClient() .prepareSearch(INDEX) .setTypes("city") .setSearchType(SearchType.QUERY_THEN_FETCH) .setScroll(new TimeValue(60000)) .setSize(100).setExplain(true) .setPostFilter(queryBuilder) .addSort(SortBuilders.geoDistanceSort("geoPoint") .order(SortOrder.ASC) .point(geoPoint.getLat(), geoPoint.getLon()) //.unit(DistanceUnit.KILOMETERS)); Original .unit(DistanceUnit.MILES)); SearchResponse response = builder .execute() .actionGet(); SearchHit[] hits = response.getHits().getHits(); scroll: while (true) { for (SearchHit hit : hits) { Map<String, Object> result = hit.getSource(); cities.add(mapper.convertValue(result, City.class)); } response = esClient.getClient().prepareSearchScroll(response.getScrollId()).setScroll(new TimeValue(60000)).execute().actionGet(); if (response.getHits().getHits().length == 0) { break scroll; } } return cities; } 

The file "LocationFinder \ src \ main \ resources \ json \ cities.json" contains all cities from Belgium. You can also delete or create entries if you wish. As long as you do not change the names and / or structure, no code changes are required.

Be sure to read README https://github.com/GlennVanSchil/LocationFinder

0
source share

All Articles