Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

linting and cleanup, take 1 #15

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion geojson/__init__.py
Original file line number Diff line number Diff line change
@@ -1 +1 @@
from conversion import convert as geojson
from conversion import convert as geojson
28 changes: 15 additions & 13 deletions geojson/conversion.py
Original file line number Diff line number Diff line change
@@ -1,22 +1,24 @@
from geojson import from_topo
from json import load,dump
from json import load, dump

def convert(topojson,input_name=None,geojson=None):
if isinstance(topojson,dict):

def convert(topojson, input_name=None, geojson=None):

if isinstance(topojson, dict):
parsed_geojson = topojson
elif isinstance(topojson,str) or isinstance(topojson,unicode):
elif isinstance(topojson, str) or isinstance(topojson, unicode):
in_file = open(topojson)
parsed_geojson = load(in_file)
in_file.close()
elif isinstance(topojson,file):
parsed_geojson=load(topojson)
elif isinstance(topojson, file):
parsed_geojson = load(topojson)
if input_name is None:
input_name = parsed_geojson['objects'].keys()[0]
out = from_topo(parsed_geojson,input_name)
if isinstance(geojson,str) or isinstance(geojson,unicode):
with open(geojson,'w') as f:
dump(out,f)
elif isinstance(geojson,file):
dump(out,geojson)
out = from_topo(parsed_geojson, input_name)
if isinstance(geojson, str) or isinstance(geojson, unicode):
with open(geojson, 'w') as f:
dump(out, f)
elif isinstance(geojson, file):
dump(out, geojson)
else:
return out
return out
120 changes: 73 additions & 47 deletions geojson/geojson.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,96 +10,122 @@


class Transformer:
def __init__(self,transform,arcs):

def __init__(self, transform, arcs):
self.scale = transform['scale']
self.translate = transform['translate']
self.arcs = map(self.convert_arc,arcs)
def convert_arc(self,arc):
self.arcs = map(self.convert_arc, arcs)

def convert_arc(self, arc):
out_arc = []
previous=[0,0]
previous = [0, 0]
for point in arc:
previous[0]+=point[0]
previous[1]+=point[1]
previous[0] += point[0]
previous[1] += point[1]
out_arc.append(self.convert_point(previous))
return out_arc
def reversed_arc(self,arc):
return map(None,reversed(self.arcs[~arc]))
def stitch_arcs(self,arcs):

def reversed_arc(self, arc):
return map(None, reversed(self.arcs[~arc]))

def stitch_arcs(self, arcs):
line_string = []
for arc in arcs:
if arc<0:
if arc < 0:
line = self.reversed_arc(arc)
else:
line = self.arcs[arc]
if len(line_string)>0:
if len(line_string) > 0:
if line_string[-1] == line[0]:
line_string.extend(line[1:])
else:
line_string.extend(line)
else:
line_string.extend(line)
return line_string
def stich_multi_arcs(self,arcs):
return map(self.stitch_arcs,arcs)
def convert_point(self,point):
return [point[0]*self.scale[0]+self.translate[0],point[1]*self.scale[1]+self.translate[1]]
def feature(self,feature):
out={'type':'Feature'}
out['geometry']={'type':feature['type']}
if feature['type'] in ('Point','MultiPoint'):

def stich_multi_arcs(self, arcs):
return map(self.stitch_arcs, arcs)

def convert_point(self, point):
return [
point[0] * self.scale[0] + self.translate[0],
point[1] * self.scale[1] + self.translate[1]
]

def feature(self, feature):
out = {'type': 'Feature'}
out['geometry'] = {'type': feature['type']}
if feature['type'] in ('Point', 'MultiPoint'):
out['geometry']['coordinates'] = feature['coordinates']
elif feature['type'] in ('LineString','MultiLineString','MultiPolygon','Polygon'):
elif feature['type'] in (
'LineString',
'MultiLineString',
'MultiPolygon',
'Polygon'):
out['geometry']['arcs'] = feature['arcs']
elif feature['type'] == 'GeometryCollection':
out['geometry']['geometries'] = feature['geometries']
for key in ('properties','bbox','id'):
for key in ('properties', 'bbox', 'id'):
if key in feature:
out[key] = feature[key]
out['geometry']=self.geometry(out['geometry'])
out['geometry'] = self.geometry(out['geometry'])
return out
def geometry(self,geometry):
if geometry['type']=='Point':

def geometry(self, geometry):
if geometry['type'] == 'Point':
return self.point(geometry)
elif geometry['type']=='MultiPoint':
elif geometry['type'] == 'MultiPoint':
return self.multi_point(geometry)
elif geometry['type']=='LineString':
elif geometry['type'] == 'LineString':
return self.line_string(geometry)
elif geometry['type']=='MultiLineString':
elif geometry['type'] == 'MultiLineString':
return self.multi_line_string_poly(geometry)
elif geometry['type']=='Polygon':
elif geometry['type'] == 'Polygon':
return self.multi_line_string_poly(geometry)
elif geometry['type']=='MultiPolygon':
elif geometry['type'] == 'MultiPolygon':
return self.multi_poly(geometry)
elif geometry['type']=='GeometryCollection':
elif geometry['type'] == 'GeometryCollection':
return self.geometry_collection(geometry)
def point(self,geometry):
geometry['coordinates']=self.convert_point(geometry[coordinates])

def point(self, geometry):
geometry['coordinates'] = self.convert_point(geometry['coordinates'])
return geometry
def multi_point(self,geometry):
geometry['coordinates']=map(self.convert_point,geometry[coordinates])
return geometry
def line_string(self,geometry):
geometry['coordinates']=self.stitch_arcs(geometry['arcs'])

def multi_point(self, geometry):
geometry['coordinates'] = map(
self.convert_point,
geometry['coordinates']
)
return geometry

def line_string(self, geometry):
geometry['coordinates'] = self.stitch_arcs(geometry['arcs'])
del geometry['arcs']
return geometry
def multi_line_string_poly(self,geometry):
geometry['coordinates']=self.stich_multi_arcs(geometry['arcs'])

def multi_line_string_poly(self, geometry):
geometry['coordinates'] = self.stich_multi_arcs(geometry['arcs'])
del geometry['arcs']
return geometry
def multi_poly(self,geometry):
geometry['coordinates']=map(self.stich_multi_arcs,geometry['arcs'])

def multi_poly(self, geometry):
geometry['coordinates'] = map(self.stich_multi_arcs, geometry['arcs'])
del geometry['arcs']
return geometry
def geometry_collection(self,geometry):
out = {'type':'FeatureCollection'}
out['features']=map(self.feature,geometry['geometries'])

def geometry_collection(self, geometry):
out = {'type': 'FeatureCollection'}
out['features'] = map(self.feature, geometry['geometries'])
return out
def from_topo(topo,obj_name):


def from_topo(topo, obj_name):
if obj_name in topo['objects']:
geojson = topo['objects'][obj_name]
else:
raise Exception(u"Something ain't right")
transformer = Transformer(topo['transform'],topo['arcs'])
transformer = Transformer(topo['transform'], topo['arcs'])
if geojson['type'] in TYPEGEOMETRIES:
geojson = transformer.geometry(geojson)
return geojson
return geojson
2 changes: 1 addition & 1 deletion topojson/__init__.py
Original file line number Diff line number Diff line change
@@ -1 +1 @@
from conversion import convert as topojson
from conversion import convert as topojson
57 changes: 28 additions & 29 deletions topojson/arcs.py
Original file line number Diff line number Diff line change
@@ -1,47 +1,47 @@
from hashtable import Hashtable
#import shelve
#from os import remove
from hashlib import sha1
#from tempfile import mkdtemp
from utils import point_compare


class Arcs:
def __init__(self,Q):

def __init__(self, Q):
self.coincidences = Hashtable(Q * 10)
self.arcsByPoint = Hashtable(Q * 10)
self.pointsByPoint = Hashtable(Q * 10)
#self.arc_db_path=mkdtemp()+'/arc_db'
#self.arcs= shelve.open(self.arc_db_path)
self.arcs={}
self.length=0
#self.storage_path = mkdtemp()+'/db'
#self.db = shelve.open(self.storage_path)
self.db={}
def get_index(self,point):
self.arcs = {}
self.length = 0
self.db = {}

def get_index(self, point):
return self.pointsByPoint.get(point)
def get_point_arcs(self,point):

def get_point_arcs(self, point):
return self.arcsByPoint.get(point)
def coincidence_lines(self,point):

def coincidence_lines(self, point):
return self.coincidences.get(point)
def peak(self,point):

def peak(self, point):
return self.coincidences.peak(point)
def push(self,arc):
self.arcs[str(self.length)]=arc
self.length+=1

def push(self, arc):
self.arcs[str(self.length)] = arc
self.length += 1
return self.length
def map(self,func):
#self.db.close()
#remove(self.storage_path)

def map(self, func):
out = []
for num in range(0,self.length):
for num in range(0, self.length):
out.append(func(self.arcs[str(num)]))
#self.arcs.close()
#remove(self.arc_db_path)
return out
def get_hash(self,arc):

def get_hash(self, arc):
ourhash = sha1()
ourhash.update(str(arc))
return ourhash.hexdigest()
def check(self,arcs):

def check(self, arcs):
a0 = arcs[0]
a1 = arcs[-1]
point = a0 if point_compare(a0, a1) < 0 else a1
Expand All @@ -52,8 +52,7 @@ def check(self,arcs):
else:
index = self.length
point_arcs.append(arcs)
self.db[h]=index
self.db[self.get_hash(list(reversed(arcs)))]=~index
self.db[h] = index
self.db[self.get_hash(list(reversed(arcs)))] = ~index
self.push(arcs)
return index

14 changes: 9 additions & 5 deletions topojson/bounds.py
Original file line number Diff line number Diff line change
@@ -1,11 +1,15 @@
from mytypes import Types


def bound(objects):

class Bounds(Types):

def __init__(self):
self.x0=self.y0=float('inf')
self.x1=self.y1=-float('inf')
def point (self,point):
self.x0 = self.y0 = float('inf')
self.x1 = self.y1 = -float('inf')

def point(self, point):
x = point[0]
y = point[1]
if x < self.x0:
Expand All @@ -16,6 +20,6 @@ def point (self,point):
self.y0 = y
if y > self.y1:
self.y1 = y
b=Bounds()
b = Bounds()
b.obj(objects)
return [b.x0,b.x1,b.y0,b.y1]
return [b.x0, b.x1, b.y0, b.y1]
39 changes: 25 additions & 14 deletions topojson/clockwise.py
Original file line number Diff line number Diff line change
@@ -1,25 +1,36 @@
class Clock:
def __init__(self,area):
self.area=area
def clock(self,feature):

def __init__(self, area):
self.area = area

def clock(self, feature):
if 'geometries' in feature:
feature['geometries'] = map(self.clock_geometry,feature['geometries'])
feature['geometries'] = map(
self.clock_geometry,
feature['geometries']
)
elif 'geometry' in feature:
feature['geometry']=self.clock_geometry(feature['geometry'])
feature['geometry'] = self.clock_geometry(feature['geometry'])
return feature
def clock_geometry(self,geo):

def clock_geometry(self, geo):
if 'type' in geo:
if geo['type']=='Polygon' or geo['type']=='MultiLineString':
if geo['type'] in ['Polygon', 'MultiLineString']:
geo['coordinates'] = self.clockwise_polygon(geo['coordinates'])
elif geo['type']=='MultiPolygon':
geo['coordinates'] = map(lambda x:self.clockwise_polygon(x),geo['coordinates'])
elif geo['type']=='LineString':
elif geo['type'] == 'MultiPolygon':
geo['coordinates'] = map(
lambda x: self.clockwise_polygon(x),
geo['coordinates']
)
elif geo['type'] == 'LineString':
geo['coordinates'] = self.clockwise_ring(geo['coordinates'])
return geo
def clockwise_polygon(self,rings):
return map(lambda x:self.clockwise_ring(x),rings)
def clockwise_ring(self,ring):

def clockwise_polygon(self, rings):
return map(lambda x: self.clockwise_ring(x), rings)

def clockwise_ring(self, ring):
if self.area(ring) > 0:
return list(reversed(ring))
else:
return ring
return ring
Loading