Accessing shapefile data

To access the data within the shapefiles, the pyshp Reader class is invoked by passing the respective file path property to the Reader class. The instantiated class will have both records and a shapes method, to allow access to the shapefile's attribute data and geometry data respectively:

# Read the Arena shapefile using the Reader class of the pyshp module
import shapefile
arena_shapefile = shapefile.Reader(root.arenafile)
arena_shapes = arena_shapefile.shapes()
arena_records = arena_shapefile.records()

Once the data has been read and assigned to iteratable variables, they can be iterated using for loops. Because the data accessed using the pyshp Reader records method corresponds to the data accessed using the shapes method, a loop counter generated using the enumerate function is used to match indexes between the current record and the corresponding geometry data in the list of geometries generated by the shapes method.

For the Arena shapefile geometry, the Reader shapes method returns the data as a list with coordinate pairs. As the Arena class geometry column is a POINT data type, the data can be written to the database table using a POINT(X Y) WKT template. The SRID (4326) is included at the beginning of the string, as per GeoAlchemy2 Extended WKT (EWKT) requirements.

Read more on the GeoAlcheym2 ORM here: http://geoalchemy-2.readthedocs.io/en/0.4/orm_tutorial.html.

With each loop, a new Arena class is instantiated and assigned to the variable arena. The name field is extracted from the Reader record data item located at index 6 and assigned to the arena variable, while the geometry data is extracted from the arena_shapes data item at count (that is, the current loop number) and assigned to the Arena columns called arena.longitude and arena.latitude.

These coordinates are then passed to the string format method to format the EWKT template and assigned to the arena.geom property. Once the data for the arena row has been assigned, it's added to the session using session.add. Finally, the data is written to the database using the session's commit method:

# Iterate through the Arena data read from the shapefile
for count, record in enumerate(arena_records):
arena = Arena()
arena.name = record[6]
print(arena.name)
point = arena_shapes[count].points[0]
arena.longitude = point[0]
arena.latitude = point[1]
arena.geom = 'SRID=4326;POINT({0} {1})'.format(point[0],
point[1])

session.add(arena)
session.commit()

For the State class (and the County and District classes), the name, Federal Information Processing Standards (FIPS) code, and postal code abbreviation are extracted from the attribute data using indexing. The pygeoif is used to convert the geometry first into a pygeoif MultiPolygon format and then into WKT, which is passed to a string template and written to the geom field as EWKT:

# Iterate through the State data read from the shapefile
for count, record in enumerate(state_records):
state = State()
state.name = record[1]
state.statefips = record[0]
state.stpostal = record[2]
state_geo = state_shapes[count]
gshape =
pygeoif.MultiPolygon(pygeoif.geometry.as_shape(state_geo))

state.geom = 'SRID=4326;{0}'.format(gshape.wkt)
session.add(state)
if count % 10 == 0:
session.commit()
session.commit()

Because of the large size of the geometry data for states, they are committed to the database every 10 loops. The final commit catches any remainder.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset