A Zipcode lookup package that uses the GeoNames Postal Code dataset from http://www.geonames.org . You can initialize it with a Postal Code dataset downloaded from http://download.geonames.org/export/zip .
Install with
go get github.com/fegoa89/zipcodes
Initializes a zipcodes struct. It will throw an error if:
- The file does not exist / wrong format.
- Some of the lines contain less that 12 elements (in the readme.txt of each postal code dataset, they define up to 12 elements).
- Where latitude / longitude value are contains a wrong format (string that can not be converted to
float64
).
zipcodesDataset, err := zipcodes.New("path/to/my/dataset.txt")
Looks for a zipcode inside the map interface we loaded. If the object can not be found by the zipcode, it will return an error. When a object is found, returns its zipcode, place name, administrative name, latitude and longitude:
location, err := zipcodesDataset.Lookup("10395")
Returns the line of sight distance between two zipcodes in kilometers:
location, err := zipcodesDataset.DistanceInKm("01945", "03058") // 49.87
Returns the line of sight distance between two zipcodes in miles:
location, err := zipcodesDataset.DistanceInMiles("01945", "03058") // 30.98
Calculates the distance between a zipcode and a give lat/lon in Kilometers:
location, err := zipcodesDataset.DistanceInKmToZipCode("01945", 51.4267, 13.9333) // 1.11
Calculates the distance between a zipcode and a give lat/lon in Miles:
location, err := zipcodesDataset.DistanceInMilToZipCode("01945", 51.4267, 13.9333) // 0.69
Returns a list of zipcodes within the radius of this zipcode in Kilometers:
location, err := zipcodesDataset.GetZipcodesWithinKmRadius("01945", 50) // ["03058"]
Returns a list of zipcodes within the radius of this zipcode in Miles:
location, err := zipcodesDataset.GetZipcodesWithinMlRadius("01945", 50) // ["03058"]