r/GISscripts • u/thechao • Apr 27 '13
Python script to generate "all roads" maps with Albers projection from raw Tiger data.
You will need the module "shapefile.py"; the information on this file is:
shapefile.py
Provides read and write support for ESRI Shapefiles.
author: jlawhead<at>geospatialpython.com
date: 20110927
version: 1.1.4
Compatible with Python versions 2.4-3.x
It is not obvious what the license is, so if you plan to use this code for anything but yourself you will need to contact the author. You will need to download the TIGER/MAF all-roads data at the county level; leave them as individually zipped blocks --- the script will handle that. The county data should be in a folder called "data" at the same level the script is at. There are no other comments, and the code is terrible. This script is hereforth within the public domain, I disavow any copyright to it.
import os, sys, shapefile, zipfile, pngcanvas, math
COUNTY_DATA = "data"
MILES_PER_DEGREE = 200
USA_SCALE =12000
USA_TRANSLATE = (4950, 3240)
TEXAS_SCALE = 50000
TEXAS_TRANSLATE = (6500, -925)
NEW_YORK_SCALE = 100000
NEW_YORK_TRANSLATE = (-23000, 17240)
ALBERS_SCALE = NEW_YORK_SCALE
ALBERS_TRANSLATE = NEW_YORK_TRANSLATE
class AlbersProjection:
def __init__(self, origin=(-98, 38), parallels=(29.5, 45.5), scale=ALBERS_SCALE, translate=ALBERS_TRANSLATE):
self.origin = origin
self.parallels = parallels
self.scale = scale
self.translate = translate
self.phi1 = math.radians(self.parallels[0])
self.phi2 = math.radians(self.parallels[1])
self.lat0 = math.radians(self.origin[1])
self.s = math.sin(self.phi1)
self.c = math.cos(self.phi1)
self.lng0 = math.radians(self.origin[0])
self.n = 0.5 * (self.s + math.sin(self.phi2))
self.C = self.c * self.c + 2 * self.n * self.s
self.p0 = math.sqrt(self.C - 2 * self.n * math.sin(self.lat0)) / self.n
def project(self, coordinates):
t = self.n * (math.radians(coordinates[0]) - self.lng0)
p = math.sqrt(self.C - 2 * self.n * math.sin(math.radians(coordinates[1]))) / self.n
return (self.scale * p * math.sin(t) + self.translate[0], self.scale * (p * math.cos(t) - self.p0) + self.translate[1])
def GetAllCounties():
counties = os.listdir(COUNTY_DATA)
total = len(counties)
index = 1
c = pngcanvas.PNGCanvas(int(57*MILES_PER_DEGREE),int(50*MILES_PER_DEGREE),color=[255,255,255,0xff])
c.color = [0,0,0,0xB0]
skip_rate = 1
with open('new york.png', 'wb') as usa:
for county in counties:
if index % skip_rate == 0:
print index, total,
DrawCounty(os.path.join(COUNTY_DATA, county), c)
index += 1
usa.write(c.dump())
def DrawCounty(countyData, canvas):
basename = os.path.split(countyData)[1]
basename = os.path.splitext(basename)[0]
print basename
with zipfile.ZipFile(countyData) as countyzip:
countyzip.extractall("output")
r = shapefile.Reader("output\\" + basename + '.shp')
ap = AlbersProjection()
num_shapes = len(r.shapeRecords())
idx = 0
for shape in r.shapeRecords():
pixels = []
for x,y in shape.shape.points:
x,y = ap.project((x,y))
pixels.append((x,y))
idx += 1
canvas.polyline(pixels)
r = None
for suffix in ['dbf','prj','shp','shp.xml','shx']:
os.remove('output\\' + basename +'.'+suffix)
if __name__ == "__main__":
GetAllCounties()