If the size of the full data file is too large and distances for only a handful of ZIPs is required, then running the calculations for the necessary ZIP codes only using the ZCTA centroid data file might be one helpful approach.
This software tool will allow you to calculate straight line (as the crow flies) or driving distance for any pair of US or Canada zip/postal codes. Just enter your two zip codes in the boxes above and press the "Generate" Report button. The distance can be optionally specified in miles, kilometers or nautical miles. The HERE API is used for the driving distance calculation and displayed map for the fastest route. CDXGeodata is used for the straight line distance calculation
If you need to calculate distance for many zip codes please check out our Zip Code Distance Batch Report. To analyze addresses instead of zip codes use our Routing Batch Report. Our CDXZipStream and CDXGeodata software can also calculate distances for long lists in Microsoft Excel or in other environments.
Version 9.2 of SAS contains new functions that allow a user to compute geodesic distances. The ZIPCITYDISTANCE function uses information from the SASHELP.ZIPCODE data set and computes the distance between zip code centroids for any two zip codes specified by the user. The GEODIST function allows a user to specify two coordinates in terms of latitude and longitude and computes the distance between those coordinates. Both functions use the Vincenty distance formula. ZIPCITYDISTANCE produces distances in miles to one decimal place, while GEODIST will produce distances in either miles or kilometers and is not restricted to only one decimal place.
Prior to Version 9.2, a user had to use a data step and write an equation to compute such distances. The most common method was to use the Haversine formula. The Haversine formula distances are not as accurate as the new Vincenty-based computations. The following is an example that computes the distance between the centroids of two zip codes: 12203, my residence; 27513, SAS in Cary NC.
The three distance estimates computed in the previous section with SAS functions and the Haversine formula are all straight line distances. There are occasions where that type of estimate is what you desire (for example, how far away is my house from a pollution source). There are other occasions where what you want is not the straight line distance but a driving distance. Again, there are instances where the straight line and driving distances will be close, but that is not the usual case. If Google Maps is used to find the distance between zips 12203 and 20050, the value is 376 miles (with an estimated driving time of about 6 hours and 20 minutes).
Given only one combination of coordinates (or in this case, zip codes), entering the values in Google Maps to get the driving distance and drive time is no problem. If one has a large number of coordinates, manual entering of the values on the Google Maps web site might be very time consuming. In that situation, URL Access Method within SAS can be used to access the Google Maps web site multiple times and extract both the driving distance and drive time each time the site is accessed.
The first FILENAME statement shows the minimum amount of information Google Maps requires to generate a map showing driving directions between two zip codes. The two macro variables &z1 and &z2 are substituted in the web address. The next FILENAME statement creates a FILEREF Z that is used in the next data step (the destination TEMP puts anything written to FILEREF Z into SAS WORK space and the file is deleted when the WORK library is cleared at the end of the SAS session).
Now that it has been demonstrated how to compute one distance and time, let's move to the situation of multiple zip codes and multiple uses of Google Maps using SAS. The following is an example that shows how to find the driving distance and drive time from zip 12203 to five zips that all are related in some way to user MSZ03.
In the above SAS code, the data step that reads the finds the driving distance and time from the Google Map web page is modified to produce the variable TIME as a numeric variable with a value in seconds. This would allow a SAS-based comparison of drive times between zips. The results of the SAS code are as follows.
The following is a suggestion for finding driving distances and times for a number of different pairs of addresses. The ZIPCITYDISTANCE function is also used to show a comparison between driving and straight line distances. Notice that the address in the DATALINES file look a bit different than in the previous example, no city and sate are specified. there is code in the data step to use the SAS-supplied data set SASHELP.ZIPCODE to add the city and state to all the addresses.
The above code produced the following in the output window of an interactive SAS session (if you look at the first example that used two zip codes and look at the calculated driving distance, you will see that the latitude and longitude just could be Albany, NY and Cary, NC).
These distances/times would probably be most helpful when using locations in an urban area. NOTE, the codes shown above were found at "DdDave's Mapping Stuff". The above changes resulted in this output.
Calculate the distance between two ZIP codes to show your users the distance (road distance or as the crow flies) to the nearest store or dealer. It allows your website's visitors to find the nearest dealers to their home by entering their own ZIP code.
Lets calculate the distance between 2 zip codes. You need to first generatelongitude and latitude from zip code and then you can calculate the distance between both longitude & latitude and display it in Kilometers or Miles.
If you have zip code data for your subscribers and store locations in your Marketing Cloud account, you can use a SQL Query to determine distances between them. With this method you can determine which store is closest to each of your subscribers.
This query pulls it all together. The round function and case statements do the dirty work of the calculations between the two zip codes. The ranking and partition part picks out the closest store per subscriber.
For geography types defaults to return the minimum geodesic distance between two geographies in meters,compute on the spheroid determined by the SRID.If use_spheroid isfalse, a faster spherical calculation is used.
So I knocked up quickly this VBA Function in Excel which uses Google API distance matrix function to calculate the Google Maps distance. Be sure to first replace YOUR_KEY with your personal API key obtained from here. See the VBA code here:
To get Google Maps distance between two coordinates simply use the same GetDistance function as above and replace the start and dest parameters with the coordinates in this format:
Now that we know how to leverage our newly learned GetDistance and GetDuration functions we can use them to measure the distance/duration for any routes, with any coordinates/addresses in between our starting point and our destination. For this I created 2 simple procedures MultiGetDistance and MultiGetDuration:
Geodesic Distance:It is the length of the shortest path between 2 points on any surface. In our case, the surface is the earth. Below program illustrates how to calculate geodesic distance from latitude-longitude data.
Great Circle Distance:It is the length of the shortest path between 2 points on a sphere. In this case, the earth is assumed to be a perfect sphere. Below program illustrates how to calculate great-circle distance from latitude-longitude data.
The basis for the Geometry type is a plane. The shortest path between twopoints on the plane is a straight line. That means calculations on geometries(areas, distances, lengths, intersections, etc) can be calculated usingcartesian mathematics and straight line vectors.
The basis for the SphericalGeography type is a sphere. The shortest pathbetween two points on the sphere is a great circle arc. That means thatcalculations on geographies (areas, distances, lengths, intersections, etc)must be calculated on the sphere, using more complicated mathematics. Moreaccurate measurements that take the actual spheroidal shape of the world intoaccount are not supported.
These functions convert between geometries andBing tiles. ForBing tiles, x and y refer to tile_x and tile_y. Bing Tilescan be cast to and from BigInts, using an internal representation that encodesthe zoom, x, and y efficiently:
Geopy can calculate geodesic distance between two points using thegeodesic distance or thegreat-circle distance,with a default of the geodesic distance available as the functiongeopy.distance.distance.
The following functionality, get-distance, is built to maintain coordination between the distance module and find function. It accepts error & distance objects and returns the response object if an error is received. 2b1af7f3a8