Skip to content

Commit

Permalink
Merge pull request #16 from mtmail/new-project-module
Browse files Browse the repository at this point in the history
  • Loading branch information
mtmail authored Aug 27, 2021
2 parents e93a85b + c31a78d commit 59b9ca6
Show file tree
Hide file tree
Showing 5 changed files with 103 additions and 92 deletions.
33 changes: 33 additions & 0 deletions lib/helpers.py
Original file line number Diff line number Diff line change
Expand Up @@ -109,3 +109,36 @@ def length(segment, nodelist):
distance += math.sqrt(((lat - previous[0])*lat_feet)**2 + ((lon - previous[1])*lon_feet)**2)
previous = (lat, lon)
return distance


def check_if_integers(numbers):
for number in numbers:
if not number:
return False
try: int(number)
except:
print("Non integer address: %s" % number)
return False

return True

def interpolation_type(this_from, this_to, other_from, other_to):
if not check_if_integers([this_from, this_to]):
return

if check_if_integers([other_from, other_to]):
if (int(this_from) % 2) == 0 and (int(this_to) % 2) == 0:
if (int(other_from) % 2) == 1 and (int(other_to) % 2) == 1:
return "even"

elif (int(this_from) % 2) == 1 and (int(this_to) % 2) == 1:
if (int(other_from) % 2) == 0 and (int(other_to) % 2) == 0:
return "odd"

return "all"

def create_wkt_linestring(segment):
coord_pairs = []
for _i, point in segment:
coord_pairs.append( "%f %f" % (point[1], point[0]) )
return 'LINESTRING(' + ','.join(coord_pairs) + ')'
29 changes: 29 additions & 0 deletions lib/project.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
"""
Deal with coordinate system transformations/projections
"""

try:
from osgeo import osr
except:
import osr

# Same as the contents of the *_edges.prj files
PROJCS_WKT = \
"""GEOGCS["GCS_North_American_1983",
DATUM["D_North_American_1983",
SPHEROID["GRS_1980",6378137,298.257222101]],
PRIMEM["Greenwich",0],
UNIT["Degree",0.017453292519943295]]"""

from_proj = osr.SpatialReference()
from_proj.ImportFromWkt(PROJCS_WKT)

# output to WGS84
to_proj = osr.SpatialReference()
to_proj.SetWellKnownGeogCS("EPSG:4326")

transformer = osr.CoordinateTransformation(from_proj, to_proj)

def unproject(point):
projected = transformer.TransformPoint(point[0], point[1])
return (projected[0], projected[1])
23 changes: 22 additions & 1 deletion tests/test_helpers.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import pytest

from lib.helpers import round_point, adjacent, glom
from lib.helpers import round_point, adjacent, glom, check_if_integers, interpolation_type, create_wkt_linestring

def test_round_point():
assert round_point([1.0, 1.0]) == (1.0, 1.0)
Expand All @@ -21,3 +21,24 @@ def test_glom():

# line1 + reversed line2
assert glom(line1, line2) == [[1,1], [1,2], [1,3], [2,3], [2,2]]

def test_check_if_integers():
assert check_if_integers([1, 2, 3])
assert check_if_integers(['b']) is False
assert check_if_integers([None]) is False
assert check_if_integers(['']) is False

def test_interpolation_type():
assert interpolation_type(100, 200, 101, 201) == "even"
assert interpolation_type(101, 201, 100, 200) == "odd"
assert interpolation_type(101, 202, 100, 200) == "all"

assert interpolation_type('abc', 202, 100, 200) is None
assert interpolation_type(100, 200, 'abc', 201) == "all"

def test_create_wkt_linestring():
segment = [
(1, (100, 200)),
(2, (101, 201))
]
assert(create_wkt_linestring(segment)) == 'LINESTRING(200.000000 100.000000,201.000000 101.000000)'
6 changes: 6 additions & 0 deletions tests/test_project.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
import pytest

from lib.project import unproject

def test_unproject():
assert(unproject([-76.521714, 36.330247])) == (36.330247, -76.521714)
104 changes: 13 additions & 91 deletions tiger_address_convert.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,13 +15,9 @@
import csv
import math

try:
from osgeo import osr
except:
import osr

from lib.parse import parse_shp_for_geom_and_tags
from lib.helpers import round_point, glom_all, length
from lib.project import unproject
from lib.helpers import round_point, glom_all, length, interpolation_type, create_wkt_linestring


# Sets the distance that the address ways should be from the main way, in feet.
Expand All @@ -35,26 +31,6 @@



# ====================================
# to do read .prj file for this data
# Change the PROJCS_WKT to match your datas prj file.
# ====================================
PROJCS_WKT = \
"""GEOGCS["GCS_North_American_1983",
DATUM["D_North_American_1983",
SPHEROID["GRS_1980",6378137,298.257222101]],
PRIMEM["Greenwich",0],
UNIT["Degree",0.017453292519943295]]"""

from_proj = osr.SpatialReference()
from_proj.ImportFromWkt( PROJCS_WKT )

# output to WGS84
to_proj = osr.SpatialReference()
to_proj.SetWellKnownGeogCS( "EPSG:4326" )

tr = osr.CoordinateTransformation( from_proj, to_proj )


def addressways(waylist, nodelist, first_id):
id = first_id
Expand All @@ -64,8 +40,6 @@ def addressways(waylist, nodelist, first_id):

for waykey, segments in waylist.items():
waykey = dict(waykey)
rsegments = []
lsegments = []
for segment in segments:
lsegment = []
rsegment = []
Expand Down Expand Up @@ -192,7 +166,7 @@ def addressways(waylist, nodelist, first_id):
rsegment.append( (id, rpoint) )
id += 1

#Generate the tags for ways and nodes
# Generate the tags for ways and nodes
zipr = ''
zipl = ''
name = ''
Expand All @@ -209,51 +183,11 @@ def addressways(waylist, nodelist, first_id):
county = result[1]
state = result[2]

#Write the nodes of the offset ways
if right:
rlinestring = []
for _i, point in rsegment:
rlinestring.append( "%f %f" % (point[1], point[0]) )
if left:
llinestring = []
for _i, point in lsegment:
llinestring.append( "%f %f" % (point[1], point[0]) )
if right:
rsegments.append( rsegment )
if left:
lsegments.append( lsegment )
rtofromint = right #Do the addresses convert to integers?
ltofromint = left #Do the addresses convert to integers?
# Write the nodes of the offset ways
if right:
try: rfromint = int(rfromadd)
except:
print("Non integer address: %s" % rfromadd)
rtofromint = False
try: rtoint = int(rtoadd)
except:
print("Non integer address: %s" % rtoadd)
rtofromint = False
if left:
try: lfromint = int(lfromadd)
except:
print("Non integer address: %s" % lfromadd)
ltofromint = False
try: ltoint = int(ltoadd)
except:
print("Non integer address: %s" % ltoadd)
ltofromint = False
if right:
id += 1

interpolationtype = "all"
if rtofromint:
if (rfromint % 2) == 0 and (rtoint % 2) == 0:
if ltofromint and (lfromint % 2) == 1 and (ltoint % 2) == 1:
interpolationtype = "even"
elif (rfromint % 2) == 1 and (rtoint % 2) == 1:
if ltofromint and (lfromint % 2) == 0 and (ltoint % 2) == 0:
interpolationtype = "odd"
interpolationtype = interpolation_type(rfromadd, rtoadd, lfromadd, ltoadd)

if interpolationtype is not None:
csv_lines.append({
'from': int(rfromadd),
'to': int(rtoadd),
Expand All @@ -262,20 +196,13 @@ def addressways(waylist, nodelist, first_id):
'city': county,
'state': state,
'postcode': zipr,
'geometry': 'LINESTRING(' + ','.join(rlinestring) + ')'
'geometry': create_wkt_linestring(rsegment)
})
if left:
id += 1

interpolationtype = "all"
if ltofromint:
if (lfromint % 2) == 0 and (ltoint % 2) == 0:
if rtofromint and (rfromint % 2) == 1 and (rtoint % 2) == 1:
interpolationtype = "even"
elif (lfromint % 2) == 1 and (ltoint % 2) == 1:
if rtofromint and (rfromint %2 ) == 0 and (rtoint % 2) == 0:
interpolationtype = "odd"
if left:
interpolationtype = interpolation_type(lfromadd, ltoadd, rfromadd, rtoadd)

if interpolationtype is not None:
csv_lines.append({
'from': int(lfromadd),
'to': int(ltoadd),
Expand All @@ -284,21 +211,17 @@ def addressways(waylist, nodelist, first_id):
'city': county,
'state': state,
'postcode': zipl,
'geometry': 'LINESTRING(' + ','.join(llinestring) + ')'
'geometry': create_wkt_linestring(lsegment)
})

return csv_lines

def unproject( point ):
pt = tr.TransformPoint( point[0], point[1] )
return (pt[0], pt[1])

def compile_nodelist( parsed_gisdata, first_id=1 ):
nodelist = {}

i = first_id
for geom, _tags in parsed_gisdata:
if len( geom )==0:
if len(geom) == 0:
continue

for point in geom:
Expand All @@ -311,11 +234,10 @@ def compile_nodelist( parsed_gisdata, first_id=1 ):




def compile_waylist( parsed_gisdata ):
waylist = {}

#Group by tiger:way_id
# Group by tiger:way_id
for geom, tags in parsed_gisdata:
way_key = tags.copy()
way_key = ( way_key['tiger:way_id'], tuple( [(k,v) for k,v in way_key.items()] ) )
Expand Down

0 comments on commit 59b9ca6

Please sign in to comment.