Skip to content

Commit

Permalink
Merge pull request #39 from Vectorial1024/elevation_matters
Browse files Browse the repository at this point in the history
Add option to consider elevation when merging nodes
  • Loading branch information
roelderickx authored Sep 10, 2023
2 parents 5e22dd1 + b872f62 commit 18d8abb
Show file tree
Hide file tree
Showing 3 changed files with 19 additions and 6 deletions.
4 changes: 4 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -118,6 +118,9 @@ options:
Split ways with more than the specified number of
nodes. Defaults to 1800. Any value below 2 - do not
split.
--consider-elevation If nodes have the same (X,Y) coordinates but different
Z-levels (e.g, different elevation), then they will be
given different node IDs (default: False)
--id ID ID to start counting from for the output file.
Defaults to 0.
--idfile IDFILE Read ID to start counting from from a file.
Expand Down Expand Up @@ -199,6 +202,7 @@ osmdata = ogr2osm.OsmData(translation_object)
# - max_points_in_way: --split-ways parameter
# - add_bounds: --add-bounds parameter
# - start_id: --id parameter
# - consider_elevation: --consider-elevation parameter
osmdata.process(datasource)

# 7. Instantiate either ogr2osm.OsmDataWriter or ogr2osm.PbfDataWriter and
Expand Down
7 changes: 6 additions & 1 deletion ogr2osm/ogr2osm.py
Original file line number Diff line number Diff line change
Expand Up @@ -96,6 +96,11 @@ def parse_commandline(logger):
parser.add_argument("--split-ways", dest="maxNodesPerWay", type=int, default=1800,
help="Split ways with more than the specified number of nodes. " +
"Defaults to %(default)s. Any value below 2 - do not split.")
parser.add_argument("--consider-elevation", dest="considerElevation", action="store_true",
help="If nodes have the same (X,Y) coordinates but different Z-levels " +
"(e.g, different elevation), " +
"then they will be given different node IDs " +
"(default: %(default)s)")
# ID generation options
parser.add_argument("--id", dest="id", type=int, default=0,
help="ID to start counting from for the output file. " +
Expand Down Expand Up @@ -266,7 +271,7 @@ def main():

osmdata = OsmData(translation_object, \
params.roundingDigits, params.maxNodesPerWay, params.addBounds, \
params.id, params.positiveId)
params.id, params.positiveId, params.considerElevation)

osmdata.load_start_id_from_file(params.idfile)

Expand Down
14 changes: 9 additions & 5 deletions ogr2osm/osm_data.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,14 +18,15 @@

class OsmData:
def __init__(self, translation, rounding_digits=7, max_points_in_way=1800, add_bounds=False, \
start_id=0, is_positive=False):
start_id=0, is_positive=False, consider_elevation=False):
self.logger = logging.getLogger(__program__)

# options
self.translation = translation
self.rounding_digits = rounding_digits
self.max_points_in_way = max_points_in_way
self.add_bounds = add_bounds
self.consider_elevation = consider_elevation

self.__bounds = OsmBoundary()
self.__nodes = []
Expand Down Expand Up @@ -83,14 +84,17 @@ def __round_number(self, n):
return int(round(n * 10**self.rounding_digits))


def __add_node(self, x, y, tags, is_way_member):
def __add_node(self, x, y, tags, is_way_member, z=None):
rx = self.__round_number(x)
ry = self.__round_number(y)
rz = self.__round_number(z) if self.consider_elevation and z else None

# TODO deprecated
unique_node_id = None
if is_way_member:
unique_node_id = (rx, ry)
if rz is not None:
unique_node_id = (rx, ry, rz)
else:
unique_node_id = self.translation.get_unique_node_identifier(rx, ry, tags)
# to be replaced by
Expand Down Expand Up @@ -129,7 +133,7 @@ def __add_relation(self, tags):


def __parse_point(self, ogrgeometry, tags):
return self.__add_node(ogrgeometry.GetX(), ogrgeometry.GetY(), tags, False)
return self.__add_node(ogrgeometry.GetX(), ogrgeometry.GetY(), tags, False, z=ogrgeometry.GetZ())


def __parse_multi_point(self, ogrgeometry, tags):
Expand Down Expand Up @@ -183,8 +187,8 @@ def __parse_linestring(self, ogrgeometry, tags):
nodes = []
potential_duplicate_ways = []
for i in range(ogrgeometry.GetPointCount()):
(x, y, z_unused) = ogrgeometry.GetPoint(i)
node = self.__add_node(x, y, {}, True)
(x, y, z) = ogrgeometry.GetPoint(i)
node = self.__add_node(x, y, {}, True, z=z)
if previous_node_id is None or previous_node_id != node.id:
if previous_node_id is None:
# first node: add all parent ways as potential duplicates
Expand Down

0 comments on commit 18d8abb

Please sign in to comment.