'Convert Canadian Postal Code to Longitude and Latitude
I have a list of 100 Canadian postal codes (e.g. M4B 1C7
). Is there anyway to convert these postal codes into approximate longitude and latitude values? Currently, I am using the following website to do this manually: https://geocoder.ca/
Is there a quicker way to do this in R? I came across the tidygeocoder package , but I am not sure if this will work for Canadian postal codes.
Solution 1:[1]
As noted by @Dave2e above, the data are available for download here, although you have to register and agree to the terms of use (basically, you can't redistribute the data or break any other laws: I've quoted them below for convenience). It seems weird that the postal code data isn't more conveniently available, but apparently its intellectual property status is contentious - Canada Post sells access to the database.
This solution may not be robust into the future: I have used other sources for this data before, which have then disappeared from the web. The files were available as of 18 April 2021.
Once you've downloaded and unzipped the CSV files, it's pretty straightforward.
library(tidyverse)
cc <- (read_csv("CanadianPostalCodes202104.csv")
## if all you want is lat/long: file also includes city, province abbreviation,
## time zone - maybe useful for troubleshooting/proofreading
%>% select(POSTAL_CODE, LATITUDE, LONGITUDE)
%>% rename(postcode="POSTAL_CODE", lat="LATITUDE", lon="LONGITUDE")
)
mydata <- tibble(postcode=c("M4B 1C7","L8P 2V7"))
mydata_aug <- left_join(mydata, cc, by="postcode")
## postcode lat lon
## 1 M4B 1C7 43.7 -79.3
## 2 L8P 2V7 43.2 -79.9
A picture of the locations (this would be prettier if I bothered to put this into a proper spatial context with province boundaries etc.): I'm not sure what's up with the dozen or so locations that are apparently south of the Canada-US border. For example, this database apparently thinks that Stouffville ON is at 44.8°N 93.3°W, which according to Google maps is smack in the middle of West Bloomington, Minnesota USA: it's actually at 43°58?N 79°15?W according to Wikipedia ...
Terms of use (in part):
You may not (nor allow any third party) to, resell, incorporate Zip File into in any product or services, copy, distribute, sell, disclose, lend, transfer, convey, modify, decompile, disassemble or reverse engineer Zip File for any purpose whatsoever.
You shall not under any circumstances: (i) use information from Zip File to build a database for resale or for access by a third party in direct competition with Service Objects; (ii) allow information from Zip File to be used in any way to verify information from a third party that resells data in direct competition with Service Objects; or (iii) provide access to or information from Zip File to a third party that resells data in direct competition with Service Objects or to a third party that plans to resell to a further third party access to Zip File or information obtained from Zip File.
You agree to comply strictly with all applicable laws and regulations in connection with your use of Zip File.
Solution 2:[2]
https://pypi.org/project/pgeocode/ is extremely fast if you use the bulk submission method. The problem with pgeocode is that it really only resolves to the Forward Sortation Area (i.e. first 3 characters) not the full postal code.
The pgeocode application uses files from GeoNames.org and I see that there is a complete file (i.e. full postal code) for Canada, but I have yet to figure out how to use it. If anybody knows please let me know.
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
Solution | Source |
---|---|
Solution 1 | |
Solution 2 |