11 changed files with 189 additions and 8 deletions
@ -0,0 +1,40 @@ |
|||||||
|
package org.leolo.map.osm.extract; |
||||||
|
|
||||||
|
import org.leolo.map.osm.extract.model.MapEntry; |
||||||
|
import org.leolo.map.osm.extract.model.Node; |
||||||
|
import org.leolo.map.osm.extract.model.Relation; |
||||||
|
import org.leolo.map.osm.extract.model.Way; |
||||||
|
|
||||||
|
import java.util.Hashtable; |
||||||
|
import java.util.Map; |
||||||
|
|
||||||
|
public class ExtractResult { |
||||||
|
|
||||||
|
private Map<Long, MapEntry> allEntry = new Hashtable<>(); |
||||||
|
private Map<Long, Relation> relations = new Hashtable<>(); |
||||||
|
private Map<Long, Way> ways = new Hashtable<>(); |
||||||
|
private Map<Long, Node> nodes = new Hashtable<>(); |
||||||
|
|
||||||
|
public ExtractResult(Map<Long, MapEntry> allEntry, Map<Long, Relation> relations, Map<Long, Way> ways, Map<Long, Node> nodes) { |
||||||
|
this.allEntry = allEntry; |
||||||
|
this.relations = relations; |
||||||
|
this.ways = ways; |
||||||
|
this.nodes = nodes; |
||||||
|
} |
||||||
|
|
||||||
|
public Map<Long, MapEntry> getAllEntry() { |
||||||
|
return allEntry; |
||||||
|
} |
||||||
|
|
||||||
|
public Map<Long, Relation> getRelations() { |
||||||
|
return relations; |
||||||
|
} |
||||||
|
|
||||||
|
public Map<Long, Way> getWays() { |
||||||
|
return ways; |
||||||
|
} |
||||||
|
|
||||||
|
public Map<Long, Node> getNodes() { |
||||||
|
return nodes; |
||||||
|
} |
||||||
|
} |
||||||
@ -1,4 +1,12 @@ |
|||||||
package org.leolo.map.osm.extract; |
package org.leolo.map.osm.extract; |
||||||
|
|
||||||
|
import java.io.PrintWriter; |
||||||
|
|
||||||
public interface OutputFormat { |
public interface OutputFormat { |
||||||
|
public void writeHeader(PrintWriter output); |
||||||
|
public void writeTailer(PrintWriter output); |
||||||
|
public void writeRelation(PrintWriter output,long id, ExtractResult extractResult); |
||||||
|
public void writeWay(PrintWriter output,long id, ExtractResult extractResult); |
||||||
|
public void writeNode(PrintWriter output,long id, ExtractResult extractResult); |
||||||
|
|
||||||
} |
} |
||||||
|
|||||||
@ -1,8 +1,90 @@ |
|||||||
package org.leolo.map.osm.extract.format; |
package org.leolo.map.osm.extract.format; |
||||||
|
|
||||||
|
import org.apache.logging.log4j.LogManager; |
||||||
|
import org.apache.logging.log4j.Logger; |
||||||
|
import org.leolo.map.osm.extract.ExtractResult; |
||||||
import org.leolo.map.osm.extract.OutputFormat; |
import org.leolo.map.osm.extract.OutputFormat; |
||||||
import org.leolo.map.osm.extract.OutputFormatter; |
import org.leolo.map.osm.extract.OutputFormatter; |
||||||
|
import org.leolo.map.osm.extract.model.MapEntry; |
||||||
|
import org.leolo.map.osm.extract.model.Node; |
||||||
|
import org.leolo.map.osm.extract.model.Relation; |
||||||
|
import org.leolo.map.osm.extract.model.Way; |
||||||
|
|
||||||
|
import java.io.PrintWriter; |
||||||
|
|
||||||
@OutputFormatter(formatName = {"kml","application/vnd.google-earth.kml+xml"}) |
@OutputFormatter(formatName = {"kml","application/vnd.google-earth.kml+xml"}) |
||||||
public class KMLFormatter implements OutputFormat { |
public class KMLFormatter implements OutputFormat { |
||||||
|
private Logger log = LogManager.getLogger(); |
||||||
|
@Override |
||||||
|
public void writeHeader(PrintWriter output) { |
||||||
|
output.print("<?xml version='1.0'?><kml xmlns='http://www.opengis.net/kml/2.2' xmlns:xsi='http://www.w3.org/2001/XMLSchema-instance' xsi:schemaLocation='https://schemas.opengis.net/kml/2.3/ogckml23.xsd'>"); |
||||||
|
output.print("<Document>"); |
||||||
|
output.print("<Style id='sty_0'>"); |
||||||
|
output.print("<LineStyle><color>501400FF</color><width>3</width></LineStyle>"); |
||||||
|
output.print("</Style>"); |
||||||
|
output.print("<Style id='sty_1'><ListStyle><listItemType>checkHideChildren</listItemType></ListStyle></Style>"); |
||||||
|
} |
||||||
|
|
||||||
|
@Override |
||||||
|
public void writeTailer(PrintWriter output) { |
||||||
|
output.print("</Document></kml>"); |
||||||
|
} |
||||||
|
|
||||||
|
@Override |
||||||
|
public void writeRelation(PrintWriter output, long id, ExtractResult extractResult) { |
||||||
|
Relation rel = extractResult.getRelations().get(id); |
||||||
|
output.print(getRelationString(id, extractResult)); |
||||||
|
} |
||||||
|
|
||||||
|
private CharSequence getRelationString(long id, ExtractResult extractResult){ |
||||||
|
Relation rel = extractResult.getRelations().get(id); |
||||||
|
StringBuffer sb = new StringBuffer(); |
||||||
|
sb.append("<Folder id='rel_").append(Long.toHexString(id)).append("'>"); |
||||||
|
sb.append("<styleUrl>#sty_1</styleUrl>"); |
||||||
|
sb.append("<name><![CDATA[").append(rel.getTag("name")).append("<]]></name>"); |
||||||
|
sb.append("<description><![CDATA[<table><tr><th>Tag Name</th><th>Tag Value</th></tr>"); |
||||||
|
rel.tagSet().stream().forEach((tagName)->{ |
||||||
|
StringBuilder tag = new StringBuilder(); |
||||||
|
tag.append("<tr><td>").append(tagName).append("</td><td>").append(rel.getTag(tagName)).append("</td>"); |
||||||
|
sb.append(tag); |
||||||
|
}); |
||||||
|
sb.append("</table>]]></description>"); |
||||||
|
rel.parallelStream().forEach((member)->{ |
||||||
|
MapEntry me = extractResult.getAllEntry().get(member); |
||||||
|
if(me==null){ |
||||||
|
log.atWarn().log("Missing entry {}", id); |
||||||
|
return; |
||||||
|
} |
||||||
|
if(me instanceof Relation){ |
||||||
|
//Calling same function to create nested folder
|
||||||
|
sb.append(getRelationString(member, extractResult)); |
||||||
|
}else if(me instanceof Way){ |
||||||
|
//Handling nameless way
|
||||||
|
StringBuffer wayOut = new StringBuffer(); |
||||||
|
Way way = (Way) me; |
||||||
|
wayOut.append("<Placemark><styleUrl>#sty_0</styleUrl><LineString id='rel_").append(Long.toHexString(id)) |
||||||
|
.append("_w_").append(Long.toHexString(member)).append("'>"); |
||||||
|
wayOut.append("<coordinates>"); |
||||||
|
for(int i=0;i<way.memberCount();i++){ |
||||||
|
Node node = extractResult.getNodes().get(way.getMember(i)); |
||||||
|
wayOut.append(node.getLon()).append(",").append(node.getLat()).append(" "); |
||||||
|
} |
||||||
|
wayOut.append("</coordinates>"); |
||||||
|
wayOut.append("</LineString></Placemark>"); |
||||||
|
sb.append(wayOut); |
||||||
|
} |
||||||
|
}); |
||||||
|
sb.append("</Folder>"); |
||||||
|
return sb; |
||||||
|
} |
||||||
|
|
||||||
|
@Override |
||||||
|
public void writeWay(PrintWriter output, long id, ExtractResult extractResult) { |
||||||
|
|
||||||
|
} |
||||||
|
|
||||||
|
@Override |
||||||
|
public void writeNode(PrintWriter output, long id, ExtractResult extractResult) { |
||||||
|
|
||||||
|
} |
||||||
} |
} |
||||||
|
|||||||
Loading…
Reference in new issue