Skip to content

Commit e7f29ce

Browse files
authored
Merge pull request #14 from maxhully/master
Version used to create vtd adjacency graphs
2 parents 5023d1c + 95612cb commit e7f29ce

File tree

10 files changed

+142
-10
lines changed

10 files changed

+142
-10
lines changed

Dockerfile

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
FROM python:3.6.5
2+
3+
RUN pip install pipenv
4+
5+
COPY ./Pipfile ./Pipfile.lock /code/
6+
7+
WORKDIR /code
8+
9+
RUN pipenv install
10+
11+
COPY ./graphmaker .
12+
13+
ENTRYPOINT [ "bash" ]

docker-compose.yml

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
version: '3.3'
2+
3+
services:
4+
graphmaker:
5+
build: .
6+
volumes:
7+
- ./graphmaker:/code
8+
links:
9+
- mongo
10+
stdin_open: true
11+
tty: true
12+
mongo:
13+
image: mongo
14+
environment:
15+
MONGO_INITDB_ROOT_USERNAME: root
16+
MONGO_INITDB_ROOT_PASSWORD: example
17+
volumes:
18+
- ./mongodata:/data/db

graphmaker/Pipfile

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
[[source]]
2+
verify_ssl = true
3+
name = "pypi"
4+
url = "https://pypi.org/simple"
5+
6+
[packages]
7+
utm = "*"
8+
9+
[requires]
10+
python_version = "3.5"
11+
12+
[dev-packages]

graphmaker/Pipfile.lock

Lines changed: 28 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

graphmaker/geospatial.py

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
from collections import Counter
2+
3+
import utm
4+
5+
6+
def utm_of_point(point):
7+
print(point.y, point.x)
8+
return utm.from_latlon(point.y, point.x)[2]
9+
10+
11+
def identify_utm_zone(df):
12+
utms = map(utm_of_point, df['geometry'].centroid)
13+
utm_counts = Counter(utms)
14+
# most_common returns a list of tuples, and we want the 0,0th entry
15+
most_common = utm_counts.most_common(1)[0][0]
16+
return most_common
17+
18+
19+
def reprojected(df):
20+
utm = identify_utm_zone(df)
21+
return df.to_crs(f"+proj=utm +zone={utm} +ellps=WGS84 +datum=WGS84 +units=m +no_defs")

graphmaker/get_vtds.py

Lines changed: 1 addition & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,15 @@
11
import io
2-
import json
32
import os
43
import zipfile
54

6-
import networkx
75
import requests
86

9-
from make_graph import construct_graph_from_file
10-
117

128
def zfill2(x):
139
return str(x).zfill(2)
1410

1511

16-
fips_codes = ['01', '02'] + list(map(zfill2, range(4, 57)))
12+
fips_codes = ['01', '02', '04', '05', '06'] + list(map(zfill2, range(8, 57)))
1713

1814

1915
def state_zip_url(fips, year='2012'):
@@ -45,10 +41,6 @@ def main():
4541
continue
4642
try:
4743
download_state_vtds(fips)
48-
graph = construct_graph_from_file(shp_location(
49-
fips), 'GEOID10', ['ALAND10', 'COUNTYFP10'])
50-
with open(output_location(fips), "w+") as f:
51-
json.dump(networkx.json_graph.adjacency_data(graph), f)
5244
except Exception:
5345
print("An error occurred for FIPS code " + fips)
5446

graphmaker/make_all.py

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
from main import main
2+
import os
3+
4+
def path_to_shp(fips):
5+
return os.path.join('/code/tiger_data', fips, 'tl_2012_' + fips + '_vtd10.shp')
6+
7+
states = os.listdir('/code/tiger_data/')
8+
states = [state for state in states if len(state) == 2]
9+
10+
print(list(path_to_shp(fips) for fips in states))
11+
12+
for fips in states:
13+
main([path_to_shp(fips)])

graphmaker/make_graph.py

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,8 @@
77
from networkx.readwrite import json_graph
88
from shapely.ops import cascaded_union
99

10+
from geospatial import reprojected
11+
1012

1113
def get_list_of_data(filepath, col_name, geoid=None):
1214
"""Pull a column data from a shape or CSV file.
@@ -66,6 +68,9 @@ def construct_graph_from_df(df, geoid_col=None, cols_to_add=None, queen=False):
6668
:returns: NetworkX Graph.
6769
6870
"""
71+
# reproject to a UTM projection for accurate areas and perimeters in meters
72+
df = reprojected(df)
73+
6974
if geoid_col is not None:
7075
df = df.set_index(geoid_col)
7176

graphmaker/reports.py

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,6 @@
55
import networkx
66
import numpy
77
import scipy
8-
98
from constants import round_to
109

1110
# import planarity

tests/test_geospatial.py

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
import geopandas
2+
from graphmaker.geospatial import identify_utm_zone, reprojected, utm_of_point
3+
from shapely.geometry import Point
4+
5+
6+
def example_point():
7+
return Point(42.3, -83.7)
8+
9+
10+
def test_identify_utm_zone():
11+
point = example_point()
12+
data = [{'name': 'example point', 'geometry': point}]
13+
df = geopandas.GeoDataFrame(data, crs="+init=epsg:4326")
14+
15+
assert identify_utm_zone(df) == 17
16+
17+
18+
def test_reprojected():
19+
point = example_point()
20+
data = [{'name': 'example point', 'geometry': point}]
21+
df = geopandas.GeoDataFrame(data, crs="+init=epsg:4326")
22+
23+
reprojected_df = reprojected(df)
24+
reprojected_point = reprojected_df['geometry'][0]
25+
26+
assert reprojected_point.x != point.x and reprojected_point.y != point.y
27+
28+
29+
def test_utm_of_point():
30+
point = example_point()
31+
assert utm_of_point(point) == 17

0 commit comments

Comments
 (0)