Need to sort postal addresses in a database table in sequence - sorting

I'm developing an Android app that has postal address routing in it.
We would like to sort each address in a database table in such a way where each address can be printed out for our driver. We don't want to spend time sorting addresses ourselves when a database table would be ideal for that.
I was thinking somehow to create a primary key in the table on a number that we can easily sort in ascending order. That would make it easy to create a manifest for the driver.
I was researching and found that there was something called WOEID (Where on Earth ID) which I think represents a region but I'm not sure if it would be good for us since 2 postal address in the same neighbourhood could use the same WOEID.
For example these 3 addresses have the same WOEID:
100 Bowden St., Lowell, MA
131 Stedman St., Lowell, MA
50 Stromquest Ave., Lowell, MA
The concept of using a single number like WOEID is perfect for us but we need to sort addresses like these in order so we know which ones are closest to each other.
Maybe there is a web site we can use to send out a request from within our app providing them with the postal address and the site will return a single number representing that address in a format our app can parse.
Maybe there is a way to Geocode or convert latitude and longitude numbers into a single number we can use.
Thanks.

So the problems arise when multiple addresses share the same WOEID? I don't know how many resources are available but couldn't it be in idea to fed this data to some API like Google Maps and calculate the distance from your position to there?
If you want to avoid google a really basic approach could be to do the calculation by yourself. (Though this would be more of an estimation.)
However I got the feeling you want to find the best route over total distance so I think your problem is also related to the travelling salesman problem
Given a list of cities and their pairwise distances, the task is to
find the shortest possible tour that visits each city exactly once.

I found a web site that allows me to do a reverse Geocoding. It returns a latitude and longitude.
I experimented a bit and found that I can add the latitude and longitude together to get a single number. Since giving an exact address to the web site will give results not completely what I'm looking for I give it just a street and city and US state to locate. I can then use the latitude and longitude returned and then sort the house addresses in numeric ascending order.
For example we have many streets that are very grid like so 1 number from the adding of the latitude and longitude represent many homes on 1 particular street. On another street a block away I did the same thing. Now all I have to do is just list everything in the database table like this:
City chosen by a dropdown, US state also chosen by a dropdown, then the geocoded number for each street followed by the home numbers.
Here is the web site I used to get the Geocoding along with a sample street address:
http://where.yahooapis.com/geocode?q=stedman+st,+lowell,+ma

Related

Finding combination of trainstops through Europe

I am currently working on a school project. The objective is to find a route through Europe from trainstation to trainstation, from country to country where the names of the consecutive trainstations have to start with the letters of the alphabet consecutively and a country can only be used once. To give an example route:(Amsterdam Central, Netherlands) --> (Berlin, Germany) --> (Carcasonne, France) etc. Countries also need to be neighbouring countries. We have received a dataset in which countries and some of their specific stations are mentioned. Some of the countries don't have a large selection of stations, making it important that a certain letter is used with a certain country, because only a small selection first letters will be present for this specific country. Can someone maybe provide me with some guidance as to how I can tackle this problem. I am currently coding in python.
cheers!
- Sort countries in order of increasing number of letter choices
- Loop C over countries in order of increasing number of letter choices
- Place C in position for a random letter that it provides
- IF neighbouring positions have been populated, but they are NOT neighbours geographically
- THEN choose a different letter for those available in C
I would use a constraint solver for this. (I'm familiar with CP-SAT because I use it at work, but you have options.)
For each letter from A to Z, create a variable whose domain is the set of stations whose name starts with that letter. Create 26 corresponding country variables and, for each corresponding station variable and country variable, add a table constraint to ensure that they describe the same thing (so the table contains entries like ("Amsterdam Central, Netherlands", "Netherlands"), though you have to translate into integer indices). Add an all-different constraint on the country variables. For each pair of consecutive country variables, add a table constraint that they be neighbors.
The solver contains a powerful deduction engine that will surely pick through the possibilities much faster than brute force or simple heuristics.

How to group close addresses?

I need to group addresses given their distances.
Let's say I have a list of 8 addresses. 5 in NYC and 3 in New Jersey. From those 5 in NYC 3 are close to the MET and 2 to the WTC. Those 3 in NJ would form one group, those close to the MET another and also those close to the WTC.
I 'd like to send this address list and get the closest to each other, grouped. Is there any API from Google Maps or Bing Maps that would do that? If not, would you have any suggestions on how to solve this?
At the question below lots of ways to calculate distance are mentioned, but I wonder if is there something already created (and available) from these big companies. I wouldn't like to recalculate every address in the list every time a new one is added.
How to group latitude/longitude points that are 'close' to each other?
Also, there's another problem that was not addressed at the aforementioned question... One address can be close to a group and several other groups. For instance:
In this example I've highlighted at least 4 groups. B forms one "close group" with A/C, but also with C/F, A/E/G and E/F/D/H. So I'd also like to know those variables. To which group the address is closer, or at least I though about limiting groups by the amount of members. In my example, using my suggested approach, B would be part of either the RED or BLACK group.
You can try a quadkey and exploit it visit nearby points firstly, similar to a space filling curve. Treat the points as a binary and interleave it. Treat the index as base-4 number. Then sort the numbers.

Find the State given Latitude and Longitude Coordinates

I have a set of 900 Latitude and Longitude Coordinates-- I need a relatively simple method for finding the 'State' referred to by these coordinates. If it helps, the data is in excel.
Google provide a Geocoding service. Part of this is reverse geocoding which converts geographic coordinates into a human-readable address including States. This Demo illustrates what can be done. There are limits to what you can do with this service.
Try to use the average values as provided here. With a bit of luck, most of your 900 coordinate pairs belong to the state with the nearest center. Calculation of distances between longitude/latitude locations is explained here.
An alternative would be to use a ZIP table with US postcodes as provided here. Once you know the postcode, you know the state, don't you? I'm not sure, but each state has an interval of ZIP codes. Once you know the ZIP code of a location, you can find the interval and the state it belongs to.
A list of coordinates of US locations could help to get a more exact allocation: http://www.bcca.org/bahaivision/fast/latlong_us.html
Find the nearest location in the list and take its state as result.
Google requires that geocoding / reverse geocoding be used with maps that users can see, so if that isn't an option for you, I think the best way is to use a database with spatial functions. First, you'll need the state boundaries found for free at NationalAtlas.gov. I use SQL Server (need 2008 or 2012 versions) and you can use the STContains() method to find what state it belongs to.
A simpler solution would be to just use the ezcmd.com rest API services.
They provide two APIs:
http://ezcmd.com/apps/app_geo_postal_codes#geo_postal_codes_api
1) All you have to do is just give it a zip code and a country code (for usa you either use US or USA) and optionally you'll pass the distance radius, and units (Miles or Km) and it'll return all other zip codes with state and province that are within the given distance
2) Free search, where you give it any fuzzy search phrase that includes either one of zip / city / state / province and country and it returns the best matches for that search phrase.
Hint: You can use #2 to find the zip code for a fuzzy (human readable) address and pass that zip code to #1 to find nearest places to that zip code.
Also they have another API that returns zip code along with full geo location information for a given IP address here:
http://ezcmd.com/apps/app_ezip_locator#ezip_locator_api
Enjoy ! I hope this helps.

How to show nearest store based on zipcode

I am creating an app tha is for a a bussines that has several stores around the state.
How can i show the information for the nearest sores based in the zip code?
Thanks for any help
The basic idea is:
Convert the ZIP code to geographical coordinates (longitude and latitude).
Compute the distance of each store to this coordinate.
Order the results by distance, ascending.
Step 2 can be optimized a bit -- for example, you might limit the search to those stores in the same state. You may also want to limit the number of stores returned if you are only going to display 10, for example.
This is about all the detail I can provide since your question is quite general.

Is there pseudocode for UK address or phone number validation?

Do you have pseudocode for field validation of the following items in the UK? I am from the USA, so I only know the ones in the USA right now.
Address Line 1
Phone Number
Mobile Number (in case they have a special rule for this, which they might not)
Post Code
Address line 1, if you want to validate what the user entered freeform, you're probably hosed. There's huge variability. You can use the PostCode Address File (see below) to assist,
Typically, if you want a "standard" address, UK-oriented websites ask for the postcode, then prompt the user to choose the correct address from all addresses at that postcode
Phone and mobile numbers. See here http://en.wikipedia.org/wiki/Telephone_numbers_in_the_United_Kingdom. A script to validate these (in several languages) can be found here: http://www.braemoor.co.uk/software/telnumbers.shtml
Post code format: http://en.wikipedia.org/wiki/UK_postcodes (contains a regular expression for validation, and refers to the Postcode Address File which lists valid addresses)
Address line 1 could be almost anything. There aren't always house numbers.
Phone numbers: the length of an area code varies. I wouldn't like to swear whether the full length is constant, but I suspect it's always at least 10 digits. Mobile numbers typically (IME) start with 07 whereas landlines typeically start with 01 or 02. Special numbers (free, local rate etc) typically start with 08. I'll try to find a reference for this. (EDIT: Again, there's a good Wikipedia article.)
Wikipedia has a good article about UK postcodes, including regular expressions for them.
Perl has the Number::Phone module that can handle UK phone numbers. Royal Mail has services for address validation and list cleansing.
For UK phone numbers, your best bet (unfortunately) is probably to download the numbering plans from Ofcom (they're excel spreadsheets, with all relevant number ranges, split up into area codes, "geographical numbers", personal numbers, mobile numbers, assorted service numbers, the different pay-for numbers and also have a mapping from number range to operator (the latter is probably NOT something you need, though).
As always with this sort of question, don't get hung up on over-validation. Check for the most likely grossly-malformed inputs then move on. Trying to keep track of what prefixes and lengths of phone number are in use in any particular locale is an enormous waste of your time. Letting a few, possibly-fixable mistakes through is better than losing customers by telling them they are ‘invalid’.
(The nearest to a standard addressing format you get in the UK is postcode plus house number. Even then there are always exceptions. )
I've started the code (or at least a bunch of RegEx patterns) for Django forms validation for GB telephone numbers here.
With a basic explanation here.

Resources