In [1]:
%matplotlib inline
import shapefile
from functools import reduce

from mpl_toolkits.basemap import Basemap
import matplotlib.pyplot as plt
import numpy as np

In [2]:
sf = shapefile.Reader("Data/cb_2015_us_zcta510_500k/cb_2015_us_zcta510_500k")

In [3]:
def find_max_and_min(shape_records):
    points = map(lambda x: x.shape.points, shape_records)
    points = reduce(lambda x, y: x + y, points)
    points = np.array(points)
    lon_min, lon_max = points[:, 0].min() - .05, points[:, 0].max() + .05
    lat_min, lat_max = points[:, 1].min() - .05, points[:, 1].max() + .05
    return lon_min, lon_max, lat_min, lat_max

def sub_polys_in_poly(poly_points):
    polys = []
    poly = []
    for index, point in enumerate(poly_points):
        poly.append(point)
        if len(poly) >= 2 and poly[0] == poly[len(poly)-1]:
            polys.append(poly)
            poly = []
    return polys

def plotShapes(plt, shape_records, colors):
    lon_min, lon_max, lat_min, lat_max = find_max_and_min(shape_records)
    
    map = Basemap(projection='merc', 
    lat_0 = np.average([lat_min, lat_max]), lon_0 = np.average([lon_min, lon_max]),
    resolution = 'c', area_thresh = 0.1,
    llcrnrlon=lon_min, llcrnrlat=lat_min,
    urcrnrlon=lon_max, urcrnrlat=lat_max)
    
    map.drawcoastlines()
    map.drawstates()
    map.fillcontinents(color = 'coral')
    map.drawmapboundary()
    
    for index, shape_record in enumerate(shape_records):
        polys = sub_polys_in_poly(shape_record.shape.points)
        for poly in polys:
            points = np.array(poly)
            x, y = map(points[:, 0], points[:,1])
            map.plot(x, y, 'o-', color=colors[index])

In [4]:
shape_records = {}
for shape_record in sf.shapeRecords():
    zip_code = shape_record.record[0]
    shape_records[zip_code] = shape_record

Metadata


In [5]:
# def of record fields
sf.fields


Out[5]:
[('DeletionFlag', 'C', 1, 0),
 ['ZCTA5CE10', 'C', 5, 0],
 ['AFFGEOID10', 'C', 14, 0],
 ['GEOID10', 'C', 5, 0],
 ['ALAND10', 'N', 14, 0],
 ['AWATER10', 'N', 14, 0]]

In [6]:
# number of shape records
len(shape_records)


Out[6]:
33144

Read zip codes

Google Maps:


In [7]:
# object toomsuba & cuba are shape records, each of them has a shape and a record property
toomsuba = shape_records['39364']
cuba = shape_records['36907']
ithaca = shape_records['14850']

In [8]:
toomsuba.record


Out[8]:
['39364', '8600000US39364', '39364', 191678251, 1426378]

In [9]:
plt.figure(figsize=(12, 12))
plotShapes(plt, [toomsuba, cuba], ['b', 'y'])
plt.show()



In [10]:
plt.figure(figsize=(12, 12))
plotShapes(plt, [ithaca], ['b'])
plt.show()


Shapely Union - Works


In [11]:
from shapely.geometry import Polygon
from shapely.ops import cascaded_union

In [12]:
# A Simple union example
triangle0 = Polygon([(0,0), (2,0), (0,2)])
triangle1 = Polygon([(1,0), (1,2), (3,0)])
triangle0.union(triangle1)


Out[12]:

In [13]:
polygon0 = Polygon(toomsuba.shape.points)
polygon1 = Polygon(cuba.shape.points)
union_poly = polygon0.union(polygon1)

In [14]:
polygon0


Out[14]:

In [15]:
polygon1


Out[15]:

In [16]:
union_poly


Out[16]:

In [17]:
# get coordinates of unoined shape
union_coords = union_poly.exterior.coords.xy
union_coords = list(zip(union_coords[0], union_coords[1]))
union_coords # [(lon, lat)]


Out[17]:
[(-88.60497, 32.426228),
 (-88.604282, 32.443011999999996),
 (-88.565697, 32.462184),
 (-88.554103, 32.464453),
 (-88.55410499999999, 32.471506999999995),
 (-88.596001, 32.471816),
 (-88.59610699999999, 32.475843999999995),
 (-88.594133, 32.478452),
 (-88.589782, 32.480802),
 (-88.58761899999999, 32.482699),
 (-88.586483, 32.484735),
 (-88.58561999999999, 32.490169),
 (-88.581694, 32.492495999999996),
 (-88.579889, 32.491427),
 (-88.572068, 32.489872),
 (-88.570065, 32.48994),
 (-88.56811599999999, 32.489163),
 (-88.561529, 32.484179),
 (-88.55860799999999, 32.482886),
 (-88.5586, 32.485479999999995),
 (-88.55803999999999, 32.490153),
 (-88.558973, 32.491723),
 (-88.558019, 32.49411),
 (-88.558599, 32.495723999999996),
 (-88.560068, 32.496967999999995),
 (-88.55959299999999, 32.502030999999995),
 (-88.558661, 32.505626),
 (-88.553052, 32.508832999999996),
 (-88.551761, 32.507751999999996),
 (-88.548851, 32.503600999999996),
 (-88.547933, 32.502912),
 (-88.543449, 32.501667999999995),
 (-88.539249, 32.499347),
 (-88.535116, 32.498264999999996),
 (-88.53032499999999, 32.499029),
 (-88.532337, 32.497859),
 (-88.53622, 32.497994),
 (-88.537172, 32.497009999999996),
 (-88.535519, 32.485347),
 (-88.531684, 32.483328),
 (-88.530031, 32.481432),
 (-88.527442, 32.479622),
 (-88.52537, 32.478881),
 (-88.525063, 32.477078),
 (-88.52384599999999, 32.475871),
 (-88.528183, 32.472031),
 (-88.528309, 32.467363),
 (-88.51955699999999, 32.467397999999996),
 (-88.51999599999999, 32.465165999999996),
 (-88.52162899999999, 32.465344),
 (-88.52331, 32.46463),
 (-88.520782, 32.464324),
 (-88.518388, 32.462137),
 (-88.515442, 32.461999),
 (-88.514848, 32.461403),
 (-88.514308, 32.459272),
 (-88.51520099999999, 32.45689),
 (-88.51644399999999, 32.456295),
 (-88.516689, 32.453249),
 (-88.514533, 32.453659),
 (-88.522545, 32.447109),
 (-88.520269, 32.44374),
 (-88.519834, 32.442478),
 (-88.496462, 32.442423),
 (-88.49342, 32.442902),
 (-88.492499, 32.442471),
 (-88.487434, 32.442489),
 (-88.486015, 32.443743999999995),
 (-88.48290899999999, 32.445105),
 (-88.480418, 32.445347999999996),
 (-88.479417, 32.446398),
 (-88.48025799999999, 32.451468),
 (-88.47652599999999, 32.451328),
 (-88.473959, 32.452462),
 (-88.472662, 32.452489),
 (-88.47050999999999, 32.454557),
 (-88.46637299999999, 32.455459999999995),
 (-88.463416, 32.454768),
 (-88.462165, 32.455366999999995),
 (-88.460663, 32.453258999999996),
 (-88.457944, 32.450432),
 (-88.45637599999999, 32.448201),
 (-88.445596, 32.453454),
 (-88.442602, 32.454318),
 (-88.441003, 32.455506),
 (-88.438599, 32.459725999999996),
 (-88.435481, 32.463639),
 (-88.430082, 32.461805),
 (-88.42708499999999, 32.460256),
 (-88.426816, 32.456409),
 (-88.42748399999999, 32.455084),
 (-88.426362, 32.453127),
 (-88.425662, 32.453182),
 (-88.42774999999999, 32.44996),
 (-88.43124499999999, 32.449388),
 (-88.431991, 32.448437),
 (-88.43686, 32.447299),
 (-88.438073, 32.445969),
 (-88.440693, 32.445529),
 (-88.44283899999999, 32.442934),
 (-88.4461, 32.441471),
 (-88.446553, 32.440256),
 (-88.44466, 32.438544),
 (-88.43979499999999, 32.435327),
 (-88.43109299999999, 32.440172),
 (-88.4039163853966, 32.448750083286896),
 (-88.40378899999999, 32.44977),
 (-88.40378899999999, 32.449885),
 (-88.39996599999999, 32.485414999999996),
 (-88.3888572465182, 32.5781229194103),
 (-88.3861753483959, 32.6005046560645),
 (-88.38563594641849, 32.60500622639189),
 (-88.382324, 32.605778),
 (-88.38189, 32.606763),
 (-88.380347, 32.606648),
 (-88.37802099999999, 32.604287),
 (-88.37661399999999, 32.604537),
 (-88.375033, 32.606083),
 (-88.37039899999999, 32.607791),
 (-88.368639, 32.607653),
 (-88.36457999999999, 32.608663),
 (-88.36515, 32.609947),
 (-88.364339, 32.611756),
 (-88.361897, 32.611803),
 (-88.357469, 32.609926),
 (-88.353445, 32.610774),
 (-88.350228, 32.612457),
 (-88.348362, 32.612691999999996),
 (-88.34736099999999, 32.61244),
 (-88.349186, 32.608511),
 (-88.34854, 32.607833),
 (-88.34568999999999, 32.608416999999996),
 (-88.343006, 32.609504),
 (-88.34248699999999, 32.607875),
 (-88.34146199999999, 32.607425),
 (-88.33868199999999, 32.609285),
 (-88.33748, 32.608829),
 (-88.33912699999999, 32.605596999999996),
 (-88.33527699999999, 32.602533),
 (-88.33290000000001, 32.599295999999995),
 (-88.329206, 32.598563),
 (-88.326909, 32.598535999999996),
 (-88.325251, 32.596877),
 (-88.323653, 32.597311999999995),
 (-88.322463, 32.599626),
 (-88.321137, 32.600702999999996),
 (-88.32019, 32.600291),
 (-88.320244, 32.597794),
 (-88.317916, 32.598596),
 (-88.316509, 32.596809),
 (-88.315237, 32.597175),
 (-88.312287, 32.599351999999996),
 (-88.31008299999999, 32.599694),
 (-88.30809699999999, 32.598960999999996),
 (-88.31150799999999, 32.596759999999996),
 (-88.31033099999999, 32.595041),
 (-88.30644099999999, 32.596877),
 (-88.300562, 32.568044),
 (-88.299874, 32.562154),
 (-88.29972599999999, 32.554770999999995),
 (-88.31656, 32.55429),
 (-88.31580199999999, 32.551311),
 (-88.316154, 32.548631),
 (-88.31445, 32.546110999999996),
 (-88.318479, 32.542766),
 (-88.318912, 32.541070999999995),
 (-88.321157, 32.541553),
 (-88.322508, 32.540821),
 (-88.32619, 32.541162),
 (-88.32805499999999, 32.542952),
 (-88.330863, 32.542992),
 (-88.331678, 32.543957999999996),
 (-88.334897, 32.545031),
 (-88.337548, 32.545356999999996),
 (-88.341335, 32.543040999999995),
 (-88.34266, 32.543315),
 (-88.345378, 32.541269),
 (-88.34487, 32.53989),
 (-88.34402399999999, 32.533989999999996),
 (-88.34325899999999, 32.532694),
 (-88.336817, 32.528473999999996),
 (-88.33452799999999, 32.525784),
 (-88.335785, 32.525264),
 (-88.33612, 32.522371),
 (-88.337035, 32.520314),
 (-88.336596, 32.51744),
 (-88.337965, 32.515231),
 (-88.340783, 32.511803),
 (-88.34431, 32.506439),
 (-88.344436, 32.504809),
 (-88.34610599999999, 32.502829999999996),
 (-88.347214, 32.502418999999996),
 (-88.345646, 32.500622),
 (-88.34556599999999, 32.496868),
 (-88.347433, 32.496181),
 (-88.34713099999999, 32.494501),
 (-88.34029199999999, 32.490091),
 (-88.33804599999999, 32.489303),
 (-88.33451699999999, 32.488856999999996),
 (-88.322885, 32.488889),
 (-88.321992, 32.485644),
 (-88.32064, 32.485272),
 (-88.319991, 32.483267999999995),
 (-88.317892, 32.483002),
 (-88.31702299999999, 32.480461999999996),
 (-88.314601, 32.479692),
 (-88.313131, 32.480415),
 (-88.313386, 32.482141999999996),
 (-88.310656, 32.481752),
 (-88.306024, 32.481716999999996),
 (-88.30653, 32.480381),
 (-88.304125, 32.479877),
 (-88.302098, 32.480748),
 (-88.30174099999999, 32.478915),
 (-88.300253, 32.47887),
 (-88.299196, 32.480269),
 (-88.295165, 32.480015),
 (-88.293785, 32.478018999999996),
 (-88.274687, 32.455374),
 (-88.27525, 32.454146),
 (-88.27765199999999, 32.452594999999995),
 (-88.28108999999999, 32.44943),
 (-88.283245, 32.450522),
 (-88.28637599999999, 32.449596),
 (-88.291772, 32.450843),
 (-88.29428, 32.450463),
 (-88.29866899999999, 32.448765),
 (-88.30039099999999, 32.448968),
 (-88.301316, 32.449947),
 (-88.307508, 32.450471),
 (-88.311387, 32.450561),
 (-88.314219, 32.449936),
 (-88.316593, 32.448673),
 (-88.321979, 32.448273),
 (-88.326791, 32.448516999999995),
 (-88.329078, 32.449123),
 (-88.33196, 32.450615),
 (-88.33438799999999, 32.450136),
 (-88.33476499999999, 32.446883),
 (-88.335602, 32.445555999999996),
 (-88.337358, 32.444708),
 (-88.338168, 32.443126),
 (-88.33730399999999, 32.442875),
 (-88.337136, 32.439856999999996),
 (-88.33818099999999, 32.439997999999996),
 (-88.33747199999999, 32.438749),
 (-88.331777, 32.436909),
 (-88.33065599999999, 32.435888999999996),
 (-88.328116, 32.430231),
 (-88.32846599999999, 32.428168),
 (-88.32819599999999, 32.425579),
 (-88.329574, 32.422655999999996),
 (-88.332398, 32.419987),
 (-88.32923, 32.419925),
 (-88.325195, 32.417909),
 (-88.322611, 32.417609),
 (-88.31970199999999, 32.418464),
 (-88.317916, 32.420359999999995),
 (-88.314252, 32.42109),
 (-88.312653, 32.422359),
 (-88.311115, 32.422582),
 (-88.30640000000001, 32.421774),
 (-88.30334099999999, 32.421811),
 (-88.301577, 32.420601999999995),
 (-88.300381, 32.418194),
 (-88.29865, 32.416849),
 (-88.29646, 32.416475),
 (-88.293601, 32.414623999999996),
 (-88.291522, 32.413928),
 (-88.288091, 32.414988),
 (-88.285071, 32.417015),
 (-88.2823, 32.416105),
 (-88.27998199999999, 32.420238999999995),
 (-88.277085, 32.418478),
 (-88.274363, 32.417809),
 (-88.270223, 32.417718),
 (-88.267392, 32.422992),
 (-88.264364, 32.428672),
 (-88.26328099999999, 32.426466999999995),
 (-88.25994, 32.424107),
 (-88.245854, 32.418003999999996),
 (-88.240428, 32.416371999999996),
 (-88.24042399999999, 32.412754),
 (-88.238755, 32.411882),
 (-88.235432, 32.411457999999996),
 (-88.228819, 32.411436),
 (-88.225371, 32.410375),
 (-88.22392599999999, 32.409448999999995),
 (-88.22064999999999, 32.405636),
 (-88.219956, 32.402932),
 (-88.21892, 32.400939),
 (-88.220765, 32.396566),
 (-88.222697, 32.392865),
 (-88.22502899999999, 32.390245),
 (-88.22513599999999, 32.388917),
 (-88.22300299999999, 32.387772),
 (-88.223947, 32.385273999999995),
 (-88.222811, 32.381059),
 (-88.222999, 32.379134),
 (-88.222161, 32.376751999999996),
 (-88.218868, 32.375997),
 (-88.217789, 32.372425),
 (-88.216304, 32.370387),
 (-88.216222, 32.368874999999996),
 (-88.215114, 32.366011),
 (-88.215735, 32.363948),
 (-88.21368299999999, 32.362439),
 (-88.212062, 32.360147999999995),
 (-88.21287099999999, 32.357833),
 (-88.21596799999999, 32.355613),
 (-88.217295, 32.355705),
 (-88.216934, 32.356972999999996),
 (-88.218188, 32.357420999999995),
 (-88.218936, 32.358841),
 (-88.223413, 32.361345),
 (-88.22390999999999, 32.36233),
 (-88.233981, 32.356802),
 (-88.238722, 32.35689),
 (-88.247343, 32.358388999999995),
 (-88.248834, 32.358885),
 (-88.25106, 32.358556),
 (-88.25271099999999, 32.35895),
 (-88.272035, 32.359431),
 (-88.273778, 32.359983),
 (-88.274469, 32.362857),
 (-88.274349, 32.365550999999996),
 (-88.27608599999999, 32.370430999999996),
 (-88.27808399999999, 32.370798),
 (-88.279162, 32.373936),
 (-88.279078, 32.375566),
 (-88.28069599999999, 32.379756),
 (-88.281882, 32.386224),
 (-88.293402, 32.388284),
 (-88.30346899999999, 32.394103),
 (-88.308, 32.395761),
 (-88.317011, 32.395828),
 (-88.315209, 32.394134),
 (-88.316165, 32.392308),
 (-88.315882, 32.39051),
 (-88.311396, 32.389038),
 (-88.310886, 32.387974),
 (-88.30832, 32.386359999999996),
 (-88.307756, 32.385317),
 (-88.308714, 32.382267),
 (-88.30732599999999, 32.380837),
 (-88.30698699999999, 32.378855),
 (-88.307718, 32.377832999999995),
 (-88.308461, 32.373942),
 (-88.307249, 32.373564),
 (-88.307357, 32.370463),
 (-88.303933, 32.369825999999996),
 (-88.30216399999999, 32.369039),
 (-88.301084, 32.369679999999995),
 (-88.298572, 32.368159),
 (-88.297702, 32.366538),
 (-88.292228, 32.365532),
 (-88.290691, 32.36423),
 (-88.28748, 32.364066),
 (-88.286509, 32.362094),
 (-88.28675199999999, 32.360926),
 (-88.28475399999999, 32.359665),
 (-88.282731, 32.357287),
 (-88.28278999999999, 32.348664),
 (-88.285083, 32.340513),
 (-88.28619499999999, 32.337823),
 (-88.28880199999999, 32.3358),
 (-88.293966, 32.334325),
 (-88.318141, 32.331863999999996),
 (-88.32023, 32.330629),
 (-88.324426, 32.325331999999996),
 (-88.327032, 32.322919),
 (-88.32448099999999, 32.322759999999995),
 (-88.322862, 32.321866),
 (-88.320596, 32.319277),
 (-88.32070399999999, 32.318384),
 (-88.3193, 32.315748),
 (-88.316952, 32.314329),
 (-88.31803099999999, 32.312976),
 (-88.32002899999999, 32.313159999999996),
 (-88.32094599999999, 32.312357999999996),
 (-88.32364299999999, 32.312908),
 (-88.325262, 32.312174999999996),
 (-88.326234, 32.314259),
 (-88.328823, 32.313502),
 (-88.32863499999999, 32.314282),
 (-88.33071199999999, 32.314098),
 (-88.33335699999999, 32.316505),
 (-88.33596, 32.3151),
 (-88.33572099999999, 32.313525),
 (-88.33724099999999, 32.312528),
 (-88.338298, 32.313955),
 (-88.33940799999999, 32.313454),
 (-88.33998, 32.31463),
 (-88.343283, 32.314275),
 (-88.345316, 32.314855),
 (-88.347335, 32.311853),
 (-88.345456, 32.30867),
 (-88.344405, 32.308157),
 (-88.37594, 32.308405),
 (-88.386912, 32.308242),
 (-88.390894, 32.308487),
 (-88.421453, 32.308679999999995),
 (-88.41800160107559, 32.3381764852796),
 (-88.41977299999999, 32.337555),
 (-88.422029, 32.338066999999995),
 (-88.42749599999999, 32.341009),
 (-88.43112699999999, 32.341609999999996),
 (-88.43571999999999, 32.341725),
 (-88.43721, 32.341029999999996),
 (-88.434985, 32.339172999999995),
 (-88.436273, 32.337585),
 (-88.439882, 32.336891),
 (-88.44021699999999, 32.338099),
 (-88.438535, 32.339558),
 (-88.438677, 32.340725),
 (-88.44136, 32.341279),
 (-88.442539, 32.343097),
 (-88.44821999999999, 32.343235),
 (-88.449653, 32.342133),
 (-88.451447, 32.341754),
 (-88.454297, 32.342078),
 (-88.45633, 32.340773999999996),
 (-88.45698399999999, 32.339295),
 (-88.45891999999999, 32.338612999999995),
 (-88.46146499999999, 32.338474999999995),
 (-88.461562, 32.33638),
 (-88.462811, 32.336023),
 (-88.46330999999999, 32.333960999999995),
 (-88.465654, 32.334289999999996),
 (-88.46695, 32.337165),
 (-88.468854, 32.33717),
 (-88.470027, 32.338401),
 (-88.47178199999999, 32.337499),
 (-88.474204, 32.338653),
 (-88.47497399999999, 32.336981),
 (-88.473458, 32.335629),
 (-88.477058, 32.334694),
 (-88.477375, 32.333698),
 (-88.47940899999999, 32.333290999999996),
 (-88.48001, 32.332378),
 (-88.47889599999999, 32.331707),
 (-88.478719, 32.326875),
 (-88.478279, 32.32498),
 (-88.480482, 32.32495),
 (-88.482708, 32.326693),
 (-88.48423799999999, 32.326328),
 (-88.485714, 32.324328),
 (-88.48686, 32.320681),
 (-88.486446, 32.319379999999995),
 (-88.487932, 32.317112),
 (-88.48885399999999, 32.313913),
 (-88.502284, 32.313868),
 (-88.502684, 32.342689),
 (-88.52830999999999, 32.342639),
 (-88.52753899999999, 32.344214),
 (-88.52936299999999, 32.347376),
 (-88.52942999999999, 32.351479),
 (-88.528452, 32.357115),
 (-88.526673, 32.359623),
 (-88.525041, 32.360577),
 (-88.525825, 32.363943),
 (-88.525678, 32.366068),
 (-88.523716, 32.369291),
 (-88.522931, 32.371739999999996),
 (-88.52412799999999, 32.376363),
 (-88.52617699999999, 32.378167),
 (-88.525185, 32.37937),
 (-88.52470699999999, 32.381845999999996),
 (-88.52207299999999, 32.382797),
 (-88.521278, 32.383939),
 (-88.51782399999999, 32.386272999999996),
 (-88.517764, 32.388007),
 (-88.51914699999999, 32.390758999999996),
 (-88.519599, 32.394106),
 (-88.518607, 32.395423),
 (-88.51933799999999, 32.399156),
 (-88.554482, 32.399238),
 (-88.55448, 32.403393),
 (-88.568834, 32.398997),
 (-88.572761, 32.402685999999996),
 (-88.57667599999999, 32.404179),
 (-88.580106, 32.408674),
 (-88.5938, 32.40379),
 (-88.593998, 32.404178),
 (-88.588346, 32.406771),
 (-88.583508, 32.409307999999996),
 (-88.57772299999999, 32.413264),
 (-88.57172399999999, 32.415158999999996),
 (-88.566484, 32.415611),
 (-88.562111, 32.414757),
 (-88.55762999999999, 32.413491),
 (-88.55431899999999, 32.413044),
 (-88.55458, 32.428957),
 (-88.55604699999999, 32.427789),
 (-88.559468, 32.426649999999995),
 (-88.55942, 32.424676999999996),
 (-88.560253, 32.423153),
 (-88.561815, 32.423102),
 (-88.563485, 32.421009999999995),
 (-88.56684, 32.419111),
 (-88.56969, 32.419948),
 (-88.57159899999999, 32.419987),
 (-88.57415999999999, 32.420944),
 (-88.578442, 32.421279),
 (-88.582641, 32.420538),
 (-88.586794, 32.418531),
 (-88.588594, 32.412008),
 (-88.591607, 32.411122),
 (-88.59195799999999, 32.405322),
 (-88.594088, 32.404354),
 (-88.59504299999999, 32.409997),
 (-88.596633, 32.411963),
 (-88.595203, 32.415568),
 (-88.596904, 32.417569),
 (-88.598233, 32.420924),
 (-88.598809, 32.420494999999995),
 (-88.602037, 32.418602),
 (-88.605504, 32.414477999999995),
 (-88.60497, 32.426228)]

In [18]:
# from osgeo import ogr

In [19]:
# dataSource = ogr.Open("cb_2015_us_zcta510_500k/cb_2015_us_zcta510_500k.shp")
# layer = dataSource.GetLayer()
# print(layer.GetGeomType()) # 3 means polygons

In [20]:
# print(layer.GetFeatureCount())

In [21]:
# geometries = []
# for feature in layer:
#     geom = feature.GetGeometryRef()
#     geometries.append(geom)

In [22]:
# geo0 = geometries[0]
# geo1 = geometries[1]
# union_poly = geo0.Union(geo1)