I have a situation that's similar to what goes on in a job search engine where you type in the zipcode where you're searching for a job and the app returns jobs in that zipcode as well as in zipcodes that are 5, 10, 15, 20 or 25 miles from that zipcode, depending on preferences set by the user.
How would you calculate the neighboring locations for a zipcode?
You need to get a list of zip codes with associated longitude / latitude coordinates. Google it - there are plenty of providers.
Then take a look at this question for an algorithm of how to calculate the distance
I don't know if you can count on geonames.org to be around for the life of your app but you could use a web service like theirs to avoid reinventing the wheel.
http://www.geonames.org/export/web-services.html
I wouldn't calculate it, I would stored it as a fixed table in the database (only to change when the allocation of ZIP codes changes in a country). Make a relationship "is_neighbor_zip", which has pairs (smaller, larger). To determine whether two codes are neighboring, check in the table for specific pair. If you want all neighboring zips, it might be better to make the table symmetric.
You need to use a GIS database and ask it for ZIP codes that are nearby your current location.
You cannot simply take the ZIP code number and apply some mathematical calculations to find other nearby ZIP codes. ZIP codes are not as geographically scattered as area codes in the US, but they are not a coordinate system.
The only exception is that the ZIP+4 codes are sub-sections of the larger ZIP code. You can assume that any ZIP+4 codes that have the same ZIP code are close to each other.
I used to work on rationalizing the ZIP code handling at a company, here are some practical notes I made:
Testing ZIP codes
Hopefully has other useful info.
Whenever you create a zipcode, geocode it (e.g. google geocoder api, saving the latitude and logitude) then google the haversine formular, this will calculate the distance (as the crow flies) from a reference point, which could also be geocoded if it is a town or zipcode.
To clarify some more:
When you are retrieving records based on their location, you need to compare each longitude and latitude DECIMAL with a reference point (your users geo-coded postcode or town name)
You can query:
SELECT * FROM photos p WHERE p.long < 60 AND p.long > 50 AND p.lat > -10 AND p.lat > 10
To find all UK photos etc because the uk is between 50 and 60 degrees longitude and +-10 latitude (i might have switched long with lat, i'm fuzzy on this)
If you want to find the distance then you will need to google the haversine formula and plug in your reference values.
Hope this clears things up a little bit more, leave a comment if you need details
Related
I downloaded data showing polygons from statscan with 1643 fsa polygons (the first 3 letters of a postal code).
TWO datasets meant to be combined, hope this is clear.
DATA1 -provided to me as descriptors for specific FSA's
FSA
DATA PROVIDED
FSA1
text1
...
text ...
FSA667
text 1667
--------
--------
DATA2 - downloaded from statscan as .shp file
FSA
Polygon coords
FSA1
Cell 2
FSA1643
Cell 4
I am combining the polygons with another dataset to show layover data for each fsa. The problem is I was provided with data showing 1667 FSA's and I'm asked to produce a map that reflects their dataset (1667 items of layover information) when combined with an equal and matching number of polygons.
Effectively there are 1667-1643 = 24 missing FSA's as polygons.
Does anyone know a good source for FSA only? Other than stats canada I can't seem to find what I need. Paid and free.... I need to see what's out there and available.
Link to statscan https://www12.statcan.gc.ca/census-recensement/2021/geo/sip-pis/boundary-limites/index2021-eng.cfm?year=21
I am using leaflet.js to show the data but this is really a question about the datasets themselves. In summary I seek 1667 polygons represending canadian FSA's (forward sorting areas) and only can find 1643.
Thanks
I can successfully view and import the data I am using qgis, the issue is the data itself. I seek 1667 polygon coordinates not the 1643 I can only find online. Hopefully free... maybe paid.
I am working with Zillow neighborhood data provided freely at http://www.zillow.com/howto/api/neighborhood-boundaries.htm . I have successfully Imported the data with SRID 4120. Now I am trying to find out the neighborhoods by giving a coordinate(lat,long) and a radius. Finding a neighborhood in which my point exists is easy and is done through STIntersect method. I am actually confused with STDistance. For complete WA state data, It is giving me a maximum distaince of 4.xxx relative to any point in the wa. My question is what is the good way to find the points which are in a given radius and what is the unit.
thanx
zAfar
Got it, I was importing geography data into geometry column.
I am writing an application which has ca. 7000 european restaurants in the database and I want to show a list of the nearest ones to the user, for example all of them which are in a radius of 5km to the user.
I could do the search on the server but it then requires internet. Is it possible to save the data on the iPhone and query the database? I can't find any references for something like that in core data or iPhones SQLite.
Do I really need to program it myself with Pythagoras and stuff and calculate the distance to every restaurant on every query? Or is there some other way?
[update] I'd like to use it like I do already on the server (but to do it on the iPhone): SELECT * FROM restaurants WHERE ST_Distance_Sphere(geo, ST_GeomFromText(POINT(55.98767 57.12345), -1)) < 5000
I want the user to be able to find a restaurant even if she has no internet connection, for example when you're in a foreign country.
Read up on latitude and longitude calculations. Latitude an longitude are themselves distances expressed as arcs upon a spherical surface. If you have a database of locations expressed by latitude and longitude, then you can perform a fetch to find only those records that fall within a few degrees latitude north and south and a few degrees longitude east and west of the users current location.
So you would end up with a predicate that would be something like (psuedo-code):
latitude >= (currentLatitude-aFewMinutesOfArc)
AND
latitude <= (currentLatitude+aFewMinutesOfArc)
AND
longitude >= (currentLongitude-aFewMinutesOfArc)
AND
longitude >= (currentLongitude+aFewMinutesOfArc)
... this would create a logical box which would return all restaurant records that fell within the box. Then if you needed to calculate the exact distances you would only have to perform calculation on a handful of records out of the 7,000 you have.
I would recommend reading up on Location Services because it provides a lot of tools for handling location calculations.
As to whether to use plain SQL or Core Data, check out this previous answer on the subject. In brief, if you already know the C SQL api and your data is simple, large and static, then SQL is a good choice. If you can take time to learn Core Data and your data is complex and dynamic, then Core Data is the better choice regardless of the size of the data.
You could use GeoFire for iOS. It uses Firebase to store the data, but since Firebase maintains a local cache, it will still work without a network connection. It will handle both the GeoQueries as well as notifying you in realtime as items enter and leave your query.
[disclaimer: I work at Firebase]
What is the best way to calculate AND add a field to a data file that shows the crow-fly distance (in miles) between two zip codes for each record (250K+) in a file? THANKS
Use this page for the raw distance information. Then use this website for the formula from Adam Bellaire's response to calculate the distance.
There is no reason to use an external service as zip codes really don't change all that often.
Enjoy!
Get a Google Earth API Key and use the API to calculate distances between two zip codes in your language of choice.
UPDATE:
If a web service isn't for you, you can check my OLD Visual Basic Posting on planet source code. It has a database of zip codes, their lat/long positions, and some VB code to calculate the distances between two zip codes. The Zipcode DB will probably require some updating, and it's in a MS Access format (but you can move that data anywhere).
You could use the Yahoo Geocoding Service to first get the latitude and longitude corrodinates for each zip code, then simply use the haversine formula to get the distance between any two sets of latitude/longitude data.
Here is a c# implementation of the haversine formula.
Enjoy!
I am currently working on a map-based iPhone application and wish to display some information to the user. For my application, it makes sense to have a setting of some sort where the user can choose Miles or Kilometers. Is there a built in mechanism (maybe similar to string localization) for doing this kind of value switching so that I can avoid an if-block around each time I want to display something to the user?
Take a look at NSLocale. You should be able to get the current locale using [NSLocale currentLocale]. Then call [theLocale objectForKey: NSLocaleMeasurementSystem] and look at the results which should tell you if the users locale uses the metric system. The docs for NSLocale have a constants section which list all of the values that can be passed to the locale.
Using this you would make your own function that could be used in your program to return your distance in a locale specific way.
Based on my quick test NSLocaleUsesMetricSystem may not work for finding if the users' region uses kilometers or miles.
In the U.K, the distances are in Miles and speed in Miles per hour but NSLocaleUsesMetricSystem returns 1.
In the U.S, NSLocaleUsesMetricSystem returns 0 which is correct (distance is in Miles and speed in Miles per hour).
In India, NSLocaleUsesMetricSystemreturns 1 which is correct again (distance is in Kilometers and spee in kph).