Use this tool to find the postal codes that intersect a specified region. By default, this tool will return zip codes in the United States.
{ region_geotable : Region of Interest ? Specify the region for which you would like to find postal codes }
{ postal_code_geotable_url : URL of Postal Codes ? Specify the URL containing postal codes in spatial format }
# CrossCompute
region_geotable_path = 'navajo-nation.kmz'
postal_code_geotable_url = 'http://www2.census.gov/geo/tiger/GENZ2017/shp/cb_2017_us_zcta510_500k.zip'
target_folder = '/tmp'
import geotable
from shapely.ops import unary_union
proj4 = geotable.LONGITUDE_LATITUDE_PROJ4
region_geotable = geotable.load(region_geotable_path, target_proj4=proj4)
region_geometry = unary_union(region_geotable.geometries)
from urllib.request import urlretrieve
postal_code_geotable_path = urlretrieve(postal_code_geotable_url, '/tmp/postal-codes.shp.zip')[0]
postal_code_geotable = geotable.load(postal_code_geotable_path, target_proj4=proj4)
print('postal_code_count = %s' % len(postal_code_geotable))
has_intersections = []
is_containeds = []
for row_index, row in postal_code_geotable.iterrows():
g = row['geometry_object']
has_intersections.append(region_geometry.intersects(g))
is_containeds.append(region_geometry.contains(g))
postal_code_geotable['has_intersection'] = has_intersections
postal_code_geotable['is_contained'] = is_containeds
from os.path import join
t = postal_code_geotable
t = t[t['has_intersection']]
target_path = t.save_csv(join(target_folder, 'postal-codes-selected.csv'))
print('selected_postal_code_count = %s' % len(t))
print('selected_postal_code_geotable_path = %s' % target_path)
{ postal_code_count : Postal Code Count ? Number of postal codes in dataset downloaded from the provided URL }
{ selected_postal_code_count : Selected Postal Code Count ? Number of postal codes that intersect region of interest }
{ selected_postal_code_geotable : Selected Postal Codes ? Postal codes that intersect region of interest }