include read to android app
git-svn-id: https://osmand.googlecode.com/svn/trunk@17 e29c36b1-1cfa-d876-8d93-3434fc2bb7b8
This commit is contained in:
parent
74061331e6
commit
5f8af871f0
4 changed files with 148 additions and 116 deletions
|
@ -58,6 +58,7 @@ import com.osmand.osm.OSMSettings;
|
|||
import com.osmand.osm.Relation;
|
||||
import com.osmand.osm.Way;
|
||||
import com.osmand.osm.OSMSettings.OSMTagKey;
|
||||
import com.osmand.osm.io.OSMStorageWriter;
|
||||
import com.osmand.osm.io.OsmBaseStorage;
|
||||
|
||||
|
||||
|
@ -244,7 +245,8 @@ public class DataExtraction implements IMapLocationListener {
|
|||
MapUtils.addIdsToList(mapWays, interestedObjects);
|
||||
// MapUtils.addIdsToList(buildings, interestedObjects);
|
||||
if (DefaultLauncherConstants.writeTestOsmFile != null) {
|
||||
storage.saveStorage(new FileOutputStream(DefaultLauncherConstants.writeTestOsmFile), interestedObjects, true);
|
||||
OSMStorageWriter writer = new OSMStorageWriter(storage.getRegisteredEntities());
|
||||
writer.saveStorage(new FileOutputStream(DefaultLauncherConstants.writeTestOsmFile), interestedObjects, true);
|
||||
}
|
||||
|
||||
System.out.println();
|
||||
|
|
138
DataExtractionOSM/src/com/osmand/osm/io/OSMStorageWriter.java
Normal file
138
DataExtractionOSM/src/com/osmand/osm/io/OSMStorageWriter.java
Normal file
|
@ -0,0 +1,138 @@
|
|||
package com.osmand.osm.io;
|
||||
|
||||
import java.io.IOException;
|
||||
import java.io.OutputStream;
|
||||
import java.util.ArrayList;
|
||||
import java.util.Collection;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
import java.util.Stack;
|
||||
import java.util.Map.Entry;
|
||||
|
||||
import javax.xml.stream.XMLStreamException;
|
||||
import javax.xml.stream.XMLStreamWriter;
|
||||
|
||||
import com.osmand.osm.Entity;
|
||||
import com.osmand.osm.Node;
|
||||
import com.osmand.osm.Relation;
|
||||
import com.osmand.osm.Way;
|
||||
import com.sun.org.apache.xerces.internal.impl.PropertyManager;
|
||||
import com.sun.xml.internal.stream.writers.XMLStreamWriterImpl;
|
||||
|
||||
import static com.osmand.osm.io.OsmBaseStorage.*;
|
||||
|
||||
public class OSMStorageWriter {
|
||||
|
||||
private final Map<Long, Entity> entities;
|
||||
private final String INDENT = " ";
|
||||
private final String INDENT2 = INDENT + INDENT;
|
||||
|
||||
|
||||
public OSMStorageWriter(Map<Long, Entity> entities){
|
||||
this.entities = entities;
|
||||
}
|
||||
|
||||
|
||||
public void saveStorage(OutputStream output, Collection<Long> interestedObjects, boolean includeLinks) throws XMLStreamException, IOException {
|
||||
PropertyManager propertyManager = new PropertyManager(PropertyManager.CONTEXT_WRITER);
|
||||
// transformer.setOutputProperty(OutputKeys.INDENT, "yes");
|
||||
// String indent = "{http://xml.apache.org/xslt}indent-amount";
|
||||
// transformer.setOutputProperty(indent, "4");
|
||||
|
||||
|
||||
XMLStreamWriter streamWriter = new XMLStreamWriterImpl(output, propertyManager);
|
||||
List<Node> nodes = new ArrayList<Node>();
|
||||
List<Way> ways = new ArrayList<Way>();
|
||||
List<Relation> relations = new ArrayList<Relation>();
|
||||
if(interestedObjects == null){
|
||||
interestedObjects = entities.keySet();
|
||||
}
|
||||
Stack<Long> toResolve = new Stack<Long>();
|
||||
toResolve.addAll(interestedObjects);
|
||||
while(!toResolve.isEmpty()){
|
||||
Long l = toResolve.pop();
|
||||
if(entities.get(l) instanceof Node){
|
||||
nodes.add((Node) entities.get(l));
|
||||
} else if(entities.get(l) instanceof Way){
|
||||
ways.add((Way) entities.get(l));
|
||||
toResolve.addAll(((Way)entities.get(l)).getNodeIds());
|
||||
} else if(entities.get(l) instanceof Relation){
|
||||
relations.add((Relation) entities.get(l));
|
||||
toResolve.addAll(((Relation)entities.get(l)).getMemberIds());
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
streamWriter.writeStartDocument();
|
||||
|
||||
writeStartElement(streamWriter, ELEM_OSM, "");
|
||||
streamWriter.writeAttribute(ATTR_VERSION, "0.6");
|
||||
for(Node n : nodes){
|
||||
writeStartElement(streamWriter, ELEM_NODE, INDENT);
|
||||
streamWriter.writeAttribute(ATTR_LAT, n.getLatitude()+"");
|
||||
streamWriter.writeAttribute(ATTR_LON, n.getLongitude()+"");
|
||||
streamWriter.writeAttribute(ATTR_ID, n.getId()+"");
|
||||
writeTags(streamWriter, n);
|
||||
streamWriter.writeEndElement();
|
||||
}
|
||||
|
||||
for(Way w : ways){
|
||||
writeStartElement(streamWriter, ELEM_WAY, INDENT);
|
||||
streamWriter.writeAttribute(ATTR_ID, w.getId()+"");
|
||||
for(Long r : w.getNodeIds()){
|
||||
writeStartElement(streamWriter, ELEM_ND, INDENT2);
|
||||
streamWriter.writeAttribute(ATTR_REF, r+"");
|
||||
streamWriter.writeEndElement();
|
||||
}
|
||||
writeTags(streamWriter, w);
|
||||
streamWriter.writeEndElement();
|
||||
}
|
||||
|
||||
for(Relation r : relations){
|
||||
writeStartElement(streamWriter, ELEM_RELATION, INDENT);
|
||||
streamWriter.writeAttribute(ATTR_ID, r.getId()+"");
|
||||
for(Entry<Long, String> e : r.getMembersMap().entrySet()){
|
||||
writeStartElement(streamWriter, ELEM_MEMBER, INDENT2);
|
||||
streamWriter.writeAttribute(ATTR_REF, e.getKey()+"");
|
||||
String s = e.getValue();
|
||||
if(s == null){
|
||||
s = "";
|
||||
}
|
||||
streamWriter.writeAttribute(ATTR_ROLE, s);
|
||||
streamWriter.writeAttribute(ATTR_TYPE, getEntityType(e.getKey()));
|
||||
streamWriter.writeEndElement();
|
||||
}
|
||||
writeTags(streamWriter, r);
|
||||
streamWriter.writeEndElement();
|
||||
}
|
||||
|
||||
streamWriter.writeEndElement(); // osm
|
||||
streamWriter.writeEndDocument();
|
||||
streamWriter.flush();
|
||||
}
|
||||
|
||||
private String getEntityType(Long id){
|
||||
Entity e = entities.get(id);
|
||||
if(e instanceof Way){
|
||||
return "way";
|
||||
} else if(e instanceof Relation){
|
||||
return "relation";
|
||||
}
|
||||
return "node";
|
||||
}
|
||||
|
||||
private void writeStartElement(XMLStreamWriter writer, String name, String indent) throws XMLStreamException{
|
||||
writer.writeCharacters("\n"+indent);
|
||||
writer.writeStartElement(name);
|
||||
}
|
||||
|
||||
|
||||
private void writeTags(XMLStreamWriter writer, Entity e) throws XMLStreamException{
|
||||
for(Entry<String, String> en : e.getTags().entrySet()){
|
||||
writeStartElement(writer, ELEM_TAG, INDENT2);
|
||||
writer.writeAttribute(ATTR_K, en.getKey());
|
||||
writer.writeAttribute(ATTR_V, en.getValue());
|
||||
writer.writeEndElement();
|
||||
}
|
||||
}
|
||||
}
|
|
@ -2,22 +2,14 @@ package com.osmand.osm.io;
|
|||
|
||||
import java.io.IOException;
|
||||
import java.io.InputStream;
|
||||
import java.io.OutputStream;
|
||||
import java.util.ArrayList;
|
||||
import java.util.Collection;
|
||||
import java.util.HashSet;
|
||||
import java.util.LinkedHashMap;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
import java.util.Set;
|
||||
import java.util.Stack;
|
||||
import java.util.Map.Entry;
|
||||
|
||||
import javax.xml.parsers.ParserConfigurationException;
|
||||
import javax.xml.parsers.SAXParser;
|
||||
import javax.xml.parsers.SAXParserFactory;
|
||||
import javax.xml.stream.XMLStreamException;
|
||||
import javax.xml.stream.XMLStreamWriter;
|
||||
|
||||
import org.xml.sax.Attributes;
|
||||
import org.xml.sax.SAXException;
|
||||
|
@ -27,8 +19,6 @@ import com.osmand.osm.Entity;
|
|||
import com.osmand.osm.Node;
|
||||
import com.osmand.osm.Relation;
|
||||
import com.osmand.osm.Way;
|
||||
import com.sun.org.apache.xerces.internal.impl.PropertyManager;
|
||||
import com.sun.xml.internal.stream.writers.XMLStreamWriterImpl;
|
||||
|
||||
public class OsmBaseStorage extends DefaultHandler {
|
||||
|
||||
|
@ -114,8 +104,7 @@ public class OsmBaseStorage extends DefaultHandler {
|
|||
supportedVersions.add("0.5");
|
||||
}
|
||||
|
||||
private final String INDENT = " ";
|
||||
private final String INDENT2 = INDENT + INDENT;
|
||||
|
||||
|
||||
@Override
|
||||
public void startElement(String uri, String localName, String name, Attributes attributes) throws SAXException {
|
||||
|
@ -184,108 +173,7 @@ public class OsmBaseStorage extends DefaultHandler {
|
|||
}
|
||||
}
|
||||
|
||||
public void saveStorage(OutputStream output, Collection<Long> interestedObjects, boolean includeLinks) throws XMLStreamException, IOException {
|
||||
PropertyManager propertyManager = new PropertyManager(PropertyManager.CONTEXT_WRITER);
|
||||
// transformer.setOutputProperty(OutputKeys.INDENT, "yes");
|
||||
// String indent = "{http://xml.apache.org/xslt}indent-amount";
|
||||
// transformer.setOutputProperty(indent, "4");
|
||||
|
||||
|
||||
XMLStreamWriter streamWriter = new XMLStreamWriterImpl(output, propertyManager);
|
||||
List<Node> nodes = new ArrayList<Node>();
|
||||
List<Way> ways = new ArrayList<Way>();
|
||||
List<Relation> relations = new ArrayList<Relation>();
|
||||
if(interestedObjects == null){
|
||||
interestedObjects = entities.keySet();
|
||||
}
|
||||
Stack<Long> toResolve = new Stack<Long>();
|
||||
toResolve.addAll(interestedObjects);
|
||||
while(!toResolve.isEmpty()){
|
||||
Long l = toResolve.pop();
|
||||
if(entities.get(l) instanceof Node){
|
||||
nodes.add((Node) entities.get(l));
|
||||
} else if(entities.get(l) instanceof Way){
|
||||
ways.add((Way) entities.get(l));
|
||||
toResolve.addAll(((Way)entities.get(l)).getNodeIds());
|
||||
} else if(entities.get(l) instanceof Relation){
|
||||
relations.add((Relation) entities.get(l));
|
||||
toResolve.addAll(((Relation)entities.get(l)).getMemberIds());
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
streamWriter.writeStartDocument();
|
||||
|
||||
writeStartElement(streamWriter, ELEM_OSM, "");
|
||||
streamWriter.writeAttribute(ATTR_VERSION, "0.6");
|
||||
for(Node n : nodes){
|
||||
writeStartElement(streamWriter, ELEM_NODE, INDENT);
|
||||
streamWriter.writeAttribute(ATTR_LAT, n.getLatitude()+"");
|
||||
streamWriter.writeAttribute(ATTR_LON, n.getLongitude()+"");
|
||||
streamWriter.writeAttribute(ATTR_ID, n.getId()+"");
|
||||
writeTags(streamWriter, n);
|
||||
streamWriter.writeEndElement();
|
||||
}
|
||||
|
||||
for(Way w : ways){
|
||||
writeStartElement(streamWriter, ELEM_WAY, INDENT);
|
||||
streamWriter.writeAttribute(ATTR_ID, w.getId()+"");
|
||||
for(Long r : w.getNodeIds()){
|
||||
writeStartElement(streamWriter, ELEM_ND, INDENT2);
|
||||
streamWriter.writeAttribute(ATTR_REF, r+"");
|
||||
streamWriter.writeEndElement();
|
||||
}
|
||||
writeTags(streamWriter, w);
|
||||
streamWriter.writeEndElement();
|
||||
}
|
||||
|
||||
for(Relation r : relations){
|
||||
writeStartElement(streamWriter, ELEM_RELATION, INDENT);
|
||||
streamWriter.writeAttribute(ATTR_ID, r.getId()+"");
|
||||
for(Entry<Long, String> e : r.getMembersMap().entrySet()){
|
||||
writeStartElement(streamWriter, ELEM_MEMBER, INDENT2);
|
||||
streamWriter.writeAttribute(ATTR_REF, e.getKey()+"");
|
||||
String s = e.getValue();
|
||||
if(s == null){
|
||||
s = "";
|
||||
}
|
||||
streamWriter.writeAttribute(ATTR_ROLE, s);
|
||||
streamWriter.writeAttribute(ATTR_TYPE, getEntityType(e.getKey()));
|
||||
streamWriter.writeEndElement();
|
||||
}
|
||||
writeTags(streamWriter, r);
|
||||
streamWriter.writeEndElement();
|
||||
}
|
||||
|
||||
streamWriter.writeEndElement(); // osm
|
||||
streamWriter.writeEndDocument();
|
||||
streamWriter.flush();
|
||||
}
|
||||
|
||||
private String getEntityType(Long id){
|
||||
Entity e = entities.get(id);
|
||||
if(e instanceof Way){
|
||||
return "way";
|
||||
} else if(e instanceof Relation){
|
||||
return "relation";
|
||||
}
|
||||
return "node";
|
||||
}
|
||||
|
||||
private void writeStartElement(XMLStreamWriter writer, String name, String indent) throws XMLStreamException{
|
||||
writer.writeCharacters("\n"+indent);
|
||||
writer.writeStartElement(name);
|
||||
}
|
||||
|
||||
|
||||
private void writeTags(XMLStreamWriter writer, Entity e) throws XMLStreamException{
|
||||
for(Entry<String, String> en : e.getTags().entrySet()){
|
||||
writeStartElement(writer, ELEM_TAG, INDENT2);
|
||||
writer.writeAttribute(ATTR_K, en.getKey());
|
||||
writer.writeAttribute(ATTR_V, en.getValue());
|
||||
writer.writeEndElement();
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
|
@ -311,6 +199,10 @@ public class OsmBaseStorage extends DefaultHandler {
|
|||
public boolean acceptNodeToLoad(Node n){
|
||||
return true;
|
||||
}
|
||||
|
||||
|
||||
public Map<Long, Entity> getRegisteredEntities() {
|
||||
return entities;
|
||||
}
|
||||
|
||||
}
|
||||
|
|
|
@ -1,7 +1,7 @@
|
|||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<classpath>
|
||||
<classpathentry kind="src" path="src"/>
|
||||
<classpathentry excluding="**/DataExtraction*|**/MapPanel*|com/osmand/osm/io/" kind="src" path="use"/>
|
||||
<classpathentry excluding="**/DataExtraction*|**/MapPanel*|com/osmand/osm/io/OSMStorageWriter.java" kind="src" path="use"/>
|
||||
<classpathentry kind="src" path="gen"/>
|
||||
<classpathentry kind="con" path="com.android.ide.eclipse.adt.ANDROID_FRAMEWORK"/>
|
||||
<classpathentry kind="output" path="bin"/>
|
||||
|
|
Loading…
Reference in a new issue