Skip to content

Commit

Permalink
support format with no height values
Browse files Browse the repository at this point in the history
  • Loading branch information
yankee42 committed Apr 11, 2019
1 parent dac5017 commit 2fc8ad8
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 12 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -90,15 +90,18 @@ private static Node toOsmNode(final CourseWaypoint waypoint) {
toMap(entry -> COURSE_PREFIX + entry.getKey(), Map.Entry::getValue)
)
);
node.getTags().put("height", String.valueOf(waypoint.getHeight()));
if (waypoint.getHeight() != null) {
node.getTags().put("height", String.valueOf(waypoint.getHeight()));
}
return node;
}

public static CourseWaypoint toCourseWaypoint(final Node node) {
final String height = node.getTags().get("height");
return new CourseWaypoint(
degreeToMeter(node.getLon()),
degreeToMeter(node.getLat() * -1),
Double.parseDouble(node.getTags().get("height")),
height == null ? null : Double.parseDouble(height),
node.getTags().entrySet()
.stream()
.filter(entry -> entry.getKey().startsWith(COURSE_PREFIX))
Expand Down
27 changes: 17 additions & 10 deletions src/main/java/com/github/yankee42/courseconvert/CourseWaypoint.java
Original file line number Diff line number Diff line change
Expand Up @@ -16,30 +16,33 @@ public class CourseWaypoint {
}
private double x;
private double y;
private double height;
private Map<String, String> properties = new HashMap<>();
private Double height;
private Map<String, String> properties;

public CourseWaypoint(final double x, final double y, final double height, final Map<String, String> properties) {
public CourseWaypoint(final double x, final double y, final Double height, final Map<String, String> properties) {
this.x = x;
this.y = y;
this.height = height;
this.properties = properties;
}

public static CourseWaypoint fromElement(final Element element) {
double x = 0, y = 0, height = 0;
double x = 0, y = 0;
Double height = null;
final Map<String, String> properties = new HashMap<>();
for (final Attribute attribute : element.getAttributes()) {
if (attribute.getName().equals("pos")) {
final String[] values = attribute.getValue().split(" ");
if (values.length != 3) {
if (values.length != 2 && values.length != 3) {
throw new RuntimeException(
"Cannot parse position <" + attribute.getValue() + "> in element <" + element.getName() + ">"
);
}
x = Double.parseDouble(values[0]);
y = Double.parseDouble(values[2]);
height = Double.parseDouble(values[1]);
y = Double.parseDouble(values[values.length - 1]);
if (values.length == 3) {
height = Double.parseDouble(values[1]);
}
} else {
properties.put(attribute.getName(), attribute.getValue());
}
Expand All @@ -63,11 +66,11 @@ public void setY(final double y) {
this.y = y;
}

public double getHeight() {
public Double getHeight() {
return height;
}

public void setHeight(final double height) {
public void setHeight(final Double height) {
this.height = height;
}

Expand All @@ -81,7 +84,11 @@ public void setProperties(final Map<String, String> properties) {

public Element toJdom(final long number) {
final Element element = new Element("waypoint" + number);
element.setAttribute("pos", String.format(Locale.US, "%1$.2f %2$.2f %3$.2f", x, height, y));
if (height == null) {
element.setAttribute("pos", String.format(Locale.US, "%1$.2f %2$.2f", x, y));
} else {
element.setAttribute("pos", String.format(Locale.US, "%1$.2f %2$.2f %3$.2f", x, height, y));
}
properties.forEach(element::setAttribute);
return element;
}
Expand Down

0 comments on commit 2fc8ad8

Please sign in to comment.