I currently am working on a piece of code in Python that takes in a csv file of latitude and longitude coordinates and returns a new csv file with the corresponding county codes. How would I go about linking each coordinate with a county code? I am aware of the reverse_geocoder python package, but this only returns the name of each county. Is there any tool that returns county code?
-
County, not country? From your use name, it might be that you are looking for American counties? Over here in Englandshire we reckon we invented counties & don't automatically assume colonial counties :-) Seriously, dude, welcome aboard, but please read How to Ask. Garbage In, Garbage Out & all that. The more detailed info you give us, the better that we can help you. While this does seem to be the best site to ask, you might want to be aware of our G.I.S sister site. I also findopen data useful for map mashups, if U wan that– MawgCommented May 27, 2020 at 20:43
2 Answers
you don't mention which country you are reverse geocoding in. In some countries there are common codes for each county, in the US there is something called a FIPS code, which usually (but not always) corresponds to a county. The OpenCage Geocoder returns county_codes in countries where those are normal and FIPS codes in the US.
Here is part of an example response for coordinates 41.3976108, 2.1311837
in Barcelona, Spain, note the field county_code
"city_district": "Sarrià - Sant Gervasi",
"continent": "Europe",
"country": "Spain",
"country_code": "es",
"county": "Barcelonès",
"county_code": "B",
"neighbourhood": "les Tres Torres",
Here is part of the response for a request for 38.2544472, -104.609141
in Pueblo, Colorado, USA.
"FIPS": {
"county": "08101",
"state": "08"
}
If you have the shape definitions for the counties, you can do your own search using a simple point-in-polygon search.
This would be greatly speeded up by using a spatial index, searching the spatial extents (ie. n/s/w/e extents) and then only performing the point-in-polygon when there is an overlap in extents.
Assuming you're in the US and not working with the Aleutians, then you can probably assume Euclidean coordinates for the point-in-polygon. True point-in-polygon on a sphere is more complicated...