Skip to content

Commit b80f644

Browse files
committed
do analysis on rescaled coordinates.
this fixes the floating point arithmetic errors that were causing centroids to be occasionally calculated incorrectly. output back to geoJSON is still in input coordinate system. graphs are drawn on rescaled coordinate system, rescaled value is stored as
1 parent b0dc8af commit b80f644

File tree

2 files changed

+17
-7
lines changed

2 files changed

+17
-7
lines changed

my_graph.py

+5-3
Original file line numberDiff line numberDiff line change
@@ -137,12 +137,12 @@ def __ne__(self, other):
137137
def __hash__(self):
138138
return hash(self.nodes)
139139

140-
def geoJSON(self):
140+
def geoJSON(self,rezero):
141141
return {
142142
"type": "Feature",
143143
"geometry": {
144144
"type": "LineString",
145-
"coordinates": [list([n.x, n.y]) for n in self.nodes]
145+
"coordinates": [list([n.x+rezero[0], n.y+rezero[1]]) for n in self.nodes]
146146
},
147147
"properties": {
148148
"road": str(self.road).lower(),
@@ -238,6 +238,7 @@ def __init__(self, G=None, name="S0"):
238238
self.name = name
239239
self.cleaned = False
240240
self.roads_update = True
241+
self.rezero_vector = np.array([0,0])
241242

242243
if G is None:
243244
self.G = nx.Graph()
@@ -283,6 +284,7 @@ def copy(self):
283284
nx_copy = self.G.copy()
284285
copy = MyGraph(nx_copy)
285286
copy.name = self.name
287+
copy.rezero_vector = self.rezero_vector
286288

287289
# outerface is a side effect of the creation of inner_facelist
288290
# so we operate on that in order to not CALL inner_facelist for every
@@ -310,7 +312,7 @@ def inner_facelist(self):
310312

311313
def myedges_geoJSON(self):
312314
return json.dumps({"type": "FeatureCollection",
313-
"features": [e.geoJSON() for e in self.myedges()]})
315+
"features": [e.geoJSON(self.rezero_vector) for e in self.myedges()]})
314316

315317

316318
############################

my_graph_helpers.py

+12-4
Original file line numberDiff line numberDiff line change
@@ -928,18 +928,26 @@ def import_and_setup(filename, threshold=1, component=None,
928928
# check that threshold is a float
929929

930930
sf = shapefile.Reader(filename)
931-
myG = graphFromShapes(sf.shapes(), name, rezero)
931+
myG1 = graphFromShapes(sf.shapes(), name, rezero)
932932

933933
print("shape file loaded")
934934

935-
myG = myG.clean_up_geometry(threshold, byblock)
935+
myG1 = myG1.clean_up_geometry(threshold, byblock)
936936

937937
print("geometery cleaned up")
938938

939+
xmin = min([n.x for n in myG1.G.nodes()])
940+
ymin = min([n.y for n in myG1.G.nodes()])
941+
942+
rezero_vector = np.array([xmin, ymin])
943+
944+
myG2 = rescale_mygraph(myG1, rezero=rezero_vector)
945+
myG2.rezero_vector = rezero_vector
946+
939947
if component is None:
940-
return myG
948+
return myG2
941949
else:
942-
return myG.connected_components()[component]
950+
return myG2.connected_components()[component]
943951

944952

945953
def rescale_mygraph(myG, rezero=np.array([0, 0]), rescale=np.array([1, 1])):

0 commit comments

Comments
 (0)