Library phase 3: change feed and folder export for Locus (GPX) and OsmAnd (favourites + tracks zip)
This commit is contained in:
1 parent
cca985d2b9
commit
59447c56f2
8 files changed
+305
-3
No files matched your search
@@ -57,6 +57,84 @@ module Maptool
|
||||
end
|
||||
private_class_method :waypoint_feature
|
||||
|
||||
# The other direction, for exporting to OsmAnd/Locus (Library#export):
|
||||
# Point features → <wpt>, kind "route" lines → <rte>, every other line
|
||||
# or polygon ring → <trk>. `group` replaces every waypoint's <type>
|
||||
# (OsmAnd reads it as the favourites group); without it the point's own
|
||||
# category is kept. Colors go out as <osmand:color> (what import reads
|
||||
# back) and, on lines, gpx_style's <line><color>, which Locus reads.
|
||||
def self.from_features(features, group: nil)
|
||||
doc = REXML::Document.new
|
||||
doc << REXML::XMLDecl.new("1.0", "UTF-8")
|
||||
gpx = doc.add_element("gpx", "version" => "1.1", "creator" => "MapTool", "xmlns" => "http://www.topografix.com/GPX/1/1",
|
||||
"xmlns:osmand" => OSMAND_NAMESPACE, "xmlns:gpx_style" => GPX_STYLE_NAMESPACE)
|
||||
points, lines = features.partition { |f| f.dig("geometry", "type") == "Point" }
|
||||
points.each { |f| write_waypoint(gpx, f, group) }
|
||||
lines.each { |f| write_line(gpx, f) }
|
||||
out = +""
|
||||
doc.write(output: out)
|
||||
out
|
||||
end
|
||||
|
||||
GPX_STYLE_NAMESPACE = "http://www.topografix.com/GPX/gpx_style/0/2"
|
||||
private_constant :GPX_STYLE_NAMESPACE
|
||||
|
||||
def self.write_waypoint(gpx, feature, group)
|
||||
props = feature["properties"] || {}
|
||||
wpt = gpx.add_element("wpt", position_attributes(feature["geometry"]["coordinates"]))
|
||||
add_text(wpt, "ele", feature["geometry"]["coordinates"][2])
|
||||
add_text(wpt, "time", props["time"])
|
||||
add_text(wpt, "name", props["name"])
|
||||
add_text(wpt, "desc", props["description"])
|
||||
add_text(wpt, "type", group || props["category"])
|
||||
return unless props["color"] || props["icon"]
|
||||
|
||||
extensions = wpt.add_element("extensions")
|
||||
add_text(extensions, "osmand:color", props["color"])
|
||||
add_text(extensions, "osmand:icon", props["icon"])
|
||||
end
|
||||
private_class_method :write_waypoint
|
||||
|
||||
def self.write_line(gpx, feature)
|
||||
props = feature["properties"] || {}
|
||||
geometry = feature["geometry"]
|
||||
segments = case geometry["type"]
|
||||
when "LineString" then [geometry["coordinates"]]
|
||||
when "MultiLineString", "Polygon" then geometry["coordinates"]
|
||||
when "MultiPolygon" then geometry["coordinates"].flatten(1)
|
||||
else return
|
||||
end
|
||||
route = props["kind"] == "route"
|
||||
element = gpx.add_element(route ? "rte" : "trk")
|
||||
add_text(element, "name", props["name"])
|
||||
add_text(element, "desc", props["description"])
|
||||
add_text(element, "type", props["activity_type"])
|
||||
if props["color"]
|
||||
extensions = element.add_element("extensions")
|
||||
add_text(extensions, "osmand:color", props["color"])
|
||||
add_text(extensions.add_element("gpx_style:line"), "gpx_style:color", props["color"].delete("#"))
|
||||
end
|
||||
if route
|
||||
segments.flatten(1).each { |c| add_text(element.add_element("rtept", position_attributes(c)), "ele", c[2]) }
|
||||
else
|
||||
segments.each do |segment|
|
||||
trkseg = element.add_element("trkseg")
|
||||
segment.each { |c| add_text(trkseg.add_element("trkpt", position_attributes(c)), "ele", c[2]) }
|
||||
end
|
||||
end
|
||||
end
|
||||
private_class_method :write_line
|
||||
|
||||
def self.position_attributes(coordinates)
|
||||
{ "lat" => coordinates[1].to_s, "lon" => coordinates[0].to_s }
|
||||
end
|
||||
private_class_method :position_attributes
|
||||
|
||||
def self.add_text(parent, name, value)
|
||||
parent.add_element(name).text = value.to_s unless value.nil? || value.to_s.empty?
|
||||
end
|
||||
private_class_method :add_text
|
||||
|
||||
# <link> is plain GPX 1.1 (a wpt/trk/rte can carry any number of them),
|
||||
# not a vendor extension like waypoint_style's <osmand:*> — a newer
|
||||
# p4n2gpx export started emitting one per park4night listing photo. A
|
||||
|
||||
Reference in new issue
Block a user