borderextract-1.3/ 0000755 0001750 0000144 00000000000 11732070445 013434 5 ustar alison users borderextract-1.3/src/ 0000755 0001750 0000144 00000000000 11732070445 014223 5 ustar alison users borderextract-1.3/src/main/ 0000755 0001750 0000144 00000000000 11732070445 015147 5 ustar alison users borderextract-1.3/src/main/java/ 0000755 0001750 0000144 00000000000 11732070445 016070 5 ustar alison users borderextract-1.3/src/main/java/com/ 0000755 0001750 0000144 00000000000 11732070445 016646 5 ustar alison users borderextract-1.3/src/main/java/com/generalbytes/ 0000755 0001750 0000144 00000000000 11732070445 021332 5 ustar alison users borderextract-1.3/src/main/java/com/generalbytes/osmosis/ 0000755 0001750 0000144 00000000000 11732070445 023026 5 ustar alison users borderextract-1.3/src/main/java/com/generalbytes/osmosis/borderextract/ 0000755 0001750 0000144 00000000000 11732070445 025676 5 ustar alison users borderextract-1.3/src/main/java/com/generalbytes/osmosis/borderextract/PolygonExporter.java 0000644 0001750 0000144 00000003511 11717401124 031714 0 ustar alison users package com.generalbytes.osmosis.borderextract;
/*
Copyright (C) 2012 by GB General Bytes GmbH, Baden, Switzerland
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
THE SOFTWARE.
*/
import javax.annotation.Nonnull;
import java.io.IOException;
import java.io.Writer;
/**
* Defines a exporter interface for a single polygon.
* @author Andre Lison, GB General Bytes GmbH, 2011
*/
interface PolygonExporter {
/**
* Marshal a polygon to writer.
* @param data The full OSM data.
* @param polygon Export this polygon.
* @param writer Write marshalled polygon to this writer.
* @throws java.io.IOException Error writing to writer.
* @throws IncompleteBorderException Incomplete border encountered.
*/
void writePolygon(@Nonnull OSMData data, @Nonnull Polygon polygon, @Nonnull Writer writer)
throws IOException, IncompleteBorderException;
}
borderextract-1.3/src/main/java/com/generalbytes/osmosis/borderextract/KMLBorderListener.java 0000644 0001750 0000144 00000012026 11717401124 032024 0 ustar alison users package com.generalbytes.osmosis.borderextract;
/*
Copyright (C) 2011 by GB General Bytes GmbH, Baden, Switzerland
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
THE SOFTWARE.
*/
import java.io.*;
import java.util.logging.Logger;
import org.openstreetmap.osmosis.core.domain.v0_6.Node;
import org.openstreetmap.osmosis.core.domain.v0_6.Way;
import org.openstreetmap.osmosis.core.domain.v0_6.WayNode;
import org.openstreetmap.osmosis.core.store.NoSuchIndexElementException;
/**
* The default border listener for various administrative borders.
* @author Andre Lison, GB General Bytes GmbH, 2011
*/
public class KMLBorderListener extends ZipFileBorderListener implements PolygonExporter {
/**
* Logger.
*/
@SuppressWarnings({"UnusedDeclaration"})
private static final Logger LOG = Logger.getLogger(KMLBorderListener.class.getName());
/**
* Get the export to use for marshalling a polygon.
*
* @return The exporter.
*/
@Override
public PolygonExporter getExporter() {
return this;
}
/**
* Get the file suffix to use for a single zip file entry.
*
* @return The suffix without the leading point.
*/
@Override
public String getEntrySuffix() {
return "kml";
}
/**
* Write the Polygon KML data.
* @param data Access to the full OSM data.
* @param polygon Export this polygon.
* @param writer Write to this writer.
* @throws IOException Error writing to writer.
* @throws IncompleteBorderException Incomplete border encountered.
*/
public void writePolygon(OSMData data, Polygon polygon, Writer writer) throws IOException, IncompleteBorderException {
writer.append("");
writer.append("");
writer.append("");
for (Way way : polygon.getOuterWays()) {
writer.append("");
writer.append("").append(way.toString()).append("");
writer.append("");
writer.append("1");
writer.append("clampToGround");
writer.append("");
appendBoundary(writer, way, data);
writer.append("");
if (!polygon.getInnerWays().isEmpty()) {
writer.append("");
for (Way innerWay : polygon.getInnerWays()) {
appendBoundary(writer, innerWay, data);
}
writer.append("");
}
writer.append("");
writer.append("");
}
writer.append("");
writer.append("");
}
/**
* Create a boundary from the given way and append it to the writer.
* @param osw Append to this writer.
* @param way Create a boundary from this way.
* @param data Access to osm data.
* @return The writer.
* @throws IncompleteBorderException Thrown when a node can not be found which is referenced by the given way.
* @throws java.io.IOException Error writing to the writer.
*/
private Writer appendBoundary(Writer osw, Way way, OSMData data)
throws IncompleteBorderException, IOException {
osw.append("");
for (WayNode wayNode : way.getWayNodes()) {
try {
Node node = data.getNode(wayNode.getNodeId());
osw.append(((Double) node.getLongitude()).toString()).append(",");
osw.append(((Double) node.getLatitude()).toString()).append(" ");
} catch(NoSuchIndexElementException e) {
throw new IncompleteBorderException("Can not get node " + wayNode.getNodeId());
}
}
osw.append("");
return osw;
}
}
borderextract-1.3/src/main/java/com/generalbytes/osmosis/borderextract/BorderExtractPlugin.java 0000644 0001750 0000144 00000003325 11666714162 032502 0 ustar alison users package com.generalbytes.osmosis.borderextract;
/*
Copyright (C) 2011 by GB General Bytes GmbH, Baden, Switzerland
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
THE SOFTWARE.
*/
import java.util.HashMap;
import java.util.Map;
import org.openstreetmap.osmosis.core.pipeline.common.TaskManagerFactory;
import org.openstreetmap.osmosis.core.plugin.PluginLoader;
/**
* Register the plugin.
*/
public class BorderExtractPlugin implements PluginLoader {
@Override
public Map loadTaskFactories() {
BorderExtractTaskFactory taskFactory = new BorderExtractTaskFactory();
Map tasks = new HashMap();
tasks.put("borderextract", taskFactory);
tasks.put("be", taskFactory);
return tasks;
}
}
borderextract-1.3/src/main/java/com/generalbytes/osmosis/borderextract/OSMData.java 0000644 0001750 0000144 00000004550 11666714162 030004 0 ustar alison users package com.generalbytes.osmosis.borderextract;
/*
Copyright (C) 2011 by GB General Bytes GmbH, Baden, Switzerland
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
THE SOFTWARE.
*/
import org.openstreetmap.osmosis.core.domain.v0_6.Node;
import org.openstreetmap.osmosis.core.domain.v0_6.Way;
import org.openstreetmap.osmosis.core.store.IndexedObjectStoreReader;
/**
* Encapsulate the access to collected Openstreetmap data.
* @author Andre Lison, GB General Bytes GmbH, 2011
*/
public class OSMData {
/**
* Read nodes from object store.
*/
private IndexedObjectStoreReader nodesReader;
/**
* Read ways from object store.
*/
private IndexedObjectStoreReader waysReader;
/**
* Create a new osm data context.
* @param nodesReader For retrieving a node by id.
* @param waysReader For retrieving a way by id.
*/
public OSMData(IndexedObjectStoreReader nodesReader, IndexedObjectStoreReader waysReader) {
this.nodesReader = nodesReader;
this.waysReader = waysReader;
}
/**
* Get a node by id.
* @param nodeId The nodes id.
* @return The node.
*/
public Node getNode(long nodeId) {
return nodesReader.get(nodeId);
}
/**
* Get a way by id.
* @param wayId The ways id.
* @return The way.
*/
public Way getWay(long wayId) {
return waysReader.get(wayId);
}
}
borderextract-1.3/src/main/java/com/generalbytes/osmosis/borderextract/Polygon.java 0000644 0001750 0000144 00000004447 11717401124 030174 0 ustar alison users package com.generalbytes.osmosis.borderextract;
/*
Copyright (C) 2012 by GB General Bytes GmbH, Baden, Switzerland
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
THE SOFTWARE.
*/
import org.openstreetmap.osmosis.core.domain.v0_6.Way;
import java.util.ArrayList;
import java.util.List;
/**
* A polygon consists of a series of outer and inner ways, describing one or more areas.
*/
class Polygon {
/**
* Outer ways of the polygon, aka contour.
*/
private List outerWays = new ArrayList();
/**
* Inner ways of the polygons, aka holes.
*/
private List innerWays = new ArrayList();
/**
* A name of the polygon.
*/
private String name;
/**
* Create a new polygon.
* @param name The name of the area enclosed by the polygon.
*/
public Polygon(String name) {
this.name = name;
}
/**
* Get the holes of this polygon.
* @return The holes or an empty list.
*/
public List getInnerWays() {
return innerWays;
}
/**
* Get the outer contour of this polygon.
* @return The contour or an empty list.
*/
public List getOuterWays() {
return outerWays;
}
/**
* Get the name of the enclosed area.
* @return The name.
*/
public String getName() {
return name;
}
}
borderextract-1.3/src/main/java/com/generalbytes/osmosis/borderextract/BorderExtractTask.java 0000644 0001750 0000144 00000014442 11702537303 032137 0 ustar alison users package com.generalbytes.osmosis.borderextract;
/*
Copyright (C) 2011 by GB General Bytes GmbH, Baden, Switzerland
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
THE SOFTWARE.
*/
import java.util.ArrayList;
import java.util.List;
import org.openstreetmap.osmosis.core.container.v0_6.BoundContainer;
import org.openstreetmap.osmosis.core.container.v0_6.EntityContainer;
import org.openstreetmap.osmosis.core.container.v0_6.EntityProcessor;
import org.openstreetmap.osmosis.core.container.v0_6.NodeContainer;
import org.openstreetmap.osmosis.core.container.v0_6.RelationContainer;
import org.openstreetmap.osmosis.core.container.v0_6.WayContainer;
import org.openstreetmap.osmosis.core.domain.v0_6.Node;
import org.openstreetmap.osmosis.core.domain.v0_6.Relation;
import org.openstreetmap.osmosis.core.domain.v0_6.Way;
import org.openstreetmap.osmosis.core.store.IndexedObjectStore;
import org.openstreetmap.osmosis.core.store.IndexedObjectStoreReader;
import org.openstreetmap.osmosis.core.store.SingleClassObjectSerializationFactory;
import org.openstreetmap.osmosis.core.task.v0_6.Sink;
import java.util.logging.Logger;
/**
* Collect all nodes and ways and calls the listeners after completion.
*
* @author Andre Lison, GB General Bytes GmbH
*/
public class BorderExtractTask implements Sink, EntityProcessor {
/**
* Logger.
*/
private static final Logger LOG = Logger.getLogger(BorderExtractTask.class.getName());
/**
* Store all ways indexed by their ids.
*/
protected final IndexedObjectStore allWays;
/**
* Store all nodes indexed by their ids.
*/
protected IndexedObjectStore allNodes;
/**
* Called, when a border is found and when the entire data has streamed through
* this plugin (call to {@link #complete()}.
*/
private List listeners = new ArrayList();
/**
* Output result to this file.
*/
private String outputFileName;
/**
* Recognized admin levels.
*/
private boolean[] adminLevels;
/**
* Create a new task and initialize the object stores.
* @param outputFileName Write the result to this file.
* @param adminLevels Recognized admin levels. The array contains a true at all indexes which
* are recognized.
*/
public BorderExtractTask(String outputFileName, boolean[] adminLevels) {
this.outputFileName = outputFileName;
this.adminLevels = adminLevels;
allNodes = new IndexedObjectStore(new SingleClassObjectSerializationFactory(Node.class), "nodes");
allWays = new IndexedObjectStore(new SingleClassObjectSerializationFactory(Way.class), "ways");
}
/**
* {@inheritDoc}
*/
public void process(EntityContainer entityContainer) {
// Ask the entity container to invoke the appropriate processing method
// for the entity type.
entityContainer.process(this);
}
/**
* Does nothing.
*/
public void process(BoundContainer boundContainer) {
}
/**
* Collect all nodes.
*/
public void process(NodeContainer container) {
Node nd = container.getEntity();
allNodes.add(nd.getId(), nd);
}
/**
* Collect all ways.
*/
public void process(WayContainer container) {
Way way = container.getEntity();
allWays.add(way.getId(), way);
}
/**
* Collection all relations tagged with boundary=administrative.
* @param container Contains a relation.
*/
public void process(RelationContainer container) {
Relation relation = container.getEntity();
String boundaryTag = EntityHelper.getTag(relation, "boundary");
if ("administrative".equals(boundaryTag)) {
String adminLevel = EntityHelper.getTag(relation, "admin_level");
try {
int adminLevelInt = Integer.parseInt(adminLevel);
if (adminLevelInt >= 0 && adminLevelInt < adminLevels.length && adminLevels[adminLevelInt]) {
for (BorderListener listener : listeners) {
boolean ret = listener.borderRelationFound(relation, adminLevelInt);
if (!ret) {
break;
}
}
}
} catch (NumberFormatException nfe) {
LOG.warning("Unrecognized admin level \"" + adminLevel + "\" at " + relation);
}
}
}
/**
* Complete the task, thus write out the borders.
*/
public void complete() {
LOG.info("Plugin complete");
// all nodes and ways are collected now
allNodes.complete();
allWays.complete();
IndexedObjectStoreReader allNodesReader = allNodes.createReader();
IndexedObjectStoreReader allWaysReader = allWays.createReader();
// process borders
OSMData osmData = new OSMData(allNodesReader, allWaysReader);
for (BorderListener listener : listeners) {
listener.complete(osmData, outputFileName);
}
allNodesReader.release();
allWaysReader.release();
}
/**
* {@inheritDoc}
*/
public void release() {
if (allNodes != null) {
allNodes.release();
}
if (allWays != null) {
allWays.release();
}
}
/**
* Add a border listener.
* @param listener The listener.
*/
public void addBorderListener(BorderListener listener) {
listeners.add(listener);
}
}
././@LongLink 0000000 0000000 0000000 00000000146 00000000000 011566 L ustar root root borderextract-1.3/src/main/java/com/generalbytes/osmosis/borderextract/IncompleteBorderException.java borderextract-1.3/src/main/java/com/generalbytes/osmosis/borderextract/IncompleteBorderException.jav0000644 0001750 0000144 00000005031 11666714162 033522 0 ustar alison users package com.generalbytes.osmosis.borderextract;
/*
Copyright (C) 2011 by GB General Bytes GmbH, Baden, Switzerland
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
THE SOFTWARE.
*/
/**
* Thrown when a way or a node is missing which is needed to construct a border.
* @author Andre Lison, GB General Bytes GmbH, 2011
*/
public class IncompleteBorderException extends Exception {
/**
* Constructs a new exception with the specified detail message. The
* cause is not initialized, and may subsequently be initialized by
* a call to {@link #initCause}.
*
* @param message the detail message. The detail message is saved for
* later retrieval by the {@link #getMessage()} method.
*/
public IncompleteBorderException(String message) {
super(message);
}
/**
* Constructs a new exception with the specified detail message and
* cause.
Note that the detail message associated with
* cause is not automatically incorporated in
* this exception's detail message.
*
* @param message the detail message (which is saved for later retrieval
* by the {@link #getMessage()} method).
* @param cause the cause (which is saved for later retrieval by the
* {@link #getCause()} method). (A null value is
* permitted, and indicates that the cause is nonexistent or
* unknown.)
* @since 1.4
*/
public IncompleteBorderException(String message, Throwable cause) {
super(message, cause);
}
}
borderextract-1.3/src/main/java/com/generalbytes/osmosis/borderextract/BaseBorderListener.java 0000644 0001750 0000144 00000026232 11724635333 032270 0 ustar alison users package com.generalbytes.osmosis.borderextract;
/*
Copyright (C) 2011, 2012 by GB General Bytes GmbH, Baden, Switzerland
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
THE SOFTWARE.
*/
import com.google.common.base.Strings;
import org.openstreetmap.osmosis.core.domain.v0_6.Relation;
import org.openstreetmap.osmosis.core.domain.v0_6.RelationMember;
import org.openstreetmap.osmosis.core.domain.v0_6.Tag;
import org.openstreetmap.osmosis.core.domain.v0_6.Way;
import org.openstreetmap.osmosis.core.domain.v0_6.WayNode;
import org.openstreetmap.osmosis.core.store.NoSuchIndexElementException;
import javax.annotation.Nullable;
import java.util.ArrayList;
import java.util.Collections;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.logging.Logger;
/**
* Basic functionality useful for all listeners.
* @author Andre Lison, GB General Bytes GmbH, 2011
*/
public abstract class BaseBorderListener implements BorderListener {
/**
* Logger.
*/
@SuppressWarnings({"UnusedDeclaration"})
private static final Logger LOG = Logger.getLogger(BaseBorderListener.class.getName());
/**
* A list of all encountered relations.
*/
protected Map relations = new HashMap();
/**
* Our own tag for transferring the role from the member to the way.
*/
public static final String ROLE_TAG_NAME = "x-role";
/**
* List of desired languages.
* @see BorderListener#setLanguages(java.util.List).
*/
protected List languages = null;
/**
* Called, when a relation was found that is an administrative border.
* That clears and possible refills the already collected ways.
*
* @param relation The found relation.
* @param adminLevel The admin level of the border.
*/
@Override
public boolean borderRelationFound(Relation relation, int adminLevel) {
// LOG.info("borderRelationFound " + relation);
relations.put(relation.getId(), relation);
return true;
}
/**
* Create a polygon from a border relation.
* @param relation The relation.
* @param data Access to indexed osm data.
* @return The polygon.
* @throws IncompleteBorderException Thrown when a way can not be found which is referenced by the given relation.
*/
protected Polygon getPolygon(Relation relation, OSMData data) throws IncompleteBorderException {
String name = EntityHelper.getName(relation, languages);
if (Strings.isNullOrEmpty(name)) {
name = "rel-" + relation.getId();
}
// System.out.println("Constructing polygon for '" + name + "'");
List sortedWays = sortContainedWays(relation, data);
Polygon polygon = new Polygon(name);
Way thisWay = null;
Role role = Role.OUTER;
for (Way way : sortedWays) {
Role relRole = Role.toRole(EntityHelper.getTag(way, ROLE_TAG_NAME));
List wayNodes = way.getWayNodes();
boolean wayContinues =
thisWay != null
&& (thisWay.getWayNodes().get(thisWay.getWayNodes().size() - 1)).getNodeId() == wayNodes.get(0).getNodeId()
&& relRole == role;
if (thisWay == null || !wayContinues) {
// remove start node, if it is the same as the end node
if (thisWay != null && !thisWay.getWayNodes().isEmpty()) {
List thisWayNodes = thisWay.getWayNodes();
if (thisWayNodes.get(0).getNodeId() == thisWayNodes.get(thisWayNodes.size() - 1).getNodeId()) {
thisWayNodes.remove(0);
}
}
// create new way and add it to the right list
thisWay = new Way(way.getId(), way.getVersion(), way.getTimestamp(), way.getUser(), way.getChangesetId());
if (relRole == Role.OUTER) {
polygon.getOuterWays().add(thisWay);
} else {
polygon.getInnerWays().add(thisWay);
}
}
thisWay.getWayNodes().addAll(
wayContinues
? wayNodes.subList(1, wayNodes.size())
: wayNodes);
role = relRole;
}
return polygon;
}
/**
* Resolve all ways which are directly or
* indirectly (through other relations) referenced by the given relation.
* @param relation The relation.
* @param ways All found ways are added to this list.
* @param data The OSM data.
* @return The given ways-list.
* @throws IncompleteBorderException Error while resolving ways or relations.
*/
private List resolveWays(Relation relation, List ways, OSMData data) throws IncompleteBorderException {
for (RelationMember member : relation.getMembers()) {
try {
switch (member.getMemberType()) {
case Relation:
Relation nestedRelation = relations.get(member.getMemberId());
if (nestedRelation != null) {
resolveWays(nestedRelation, ways, data);
}
break;
case Way:
Way way = data.getWay(member.getMemberId());
List wayNodes = way.getWayNodes();
if (!wayNodes.isEmpty()) {
way.getTags().add(new Tag(ROLE_TAG_NAME, Strings.nullToEmpty(member.getMemberRole())));
ways.add(way);
}
break;
default:
break;
}
} catch (NoSuchIndexElementException e) {
throw new IncompleteBorderException("Can not find way " + member.getMemberId()
+ " for relation " + relation.getId()
+ " (" + EntityHelper.getName(relation, languages) + ")");
}
}
return ways;
}
/**
* Get all ways sorted such that subsequent ways form a closed polygon, if possible.
* @param relation Sort ways referenced by this relation.
* @param data Access to osm-data.
* @return All referenced ways sorted.
* @throws IncompleteBorderException An way referenced by the relation is missing.
*/
protected List sortContainedWays(Relation relation, OSMData data) throws IncompleteBorderException {
// System.out.println("---------------------------------------------");
// System.out.println("Sorting relation " + relation.getId());
// first retrieve all ways
List ways = new ArrayList();
resolveWays(relation, ways, data);
// System.out.println("Found " + ways.size() + " ways");
// sort ways, such that they connect to a continuous path, if possible
List sortedWays = new ArrayList(ways.size());
if (!ways.isEmpty()) {
sortedWays.add(ways.get(0));
ways.remove(0);
}
while (!ways.isEmpty()) {
Way lastWay = sortedWays.get(sortedWays.size() - 1);
Role lastRole = Role.toRole(EntityHelper.getTag(lastWay, ROLE_TAG_NAME));
// System.out.println("\nLastway=" + lastWay.getId() + ": role [" + lastRole + "]");
boolean foundNext = false;
long lastNodeId = lastWay.getWayNodes().get(lastWay.getWayNodes().size() - 1).getNodeId();
// search next way connecting to the end node with its start node
for (int i = 0; !foundNext && i < ways.size(); i++) {
Way way2 = ways.get(i);
Role role2 = Role.toRole(EntityHelper.getTag(way2, ROLE_TAG_NAME));
// System.out.println("Way2=" + way2.getId() + ": role [" + role2 + "]");
if (role2 == lastRole) {
List wayNodes = way2.getWayNodes();
// System.out.println(" last node id=" + lastNodeId
// + ": way2 (" + wayNodes.size() + " nodes):"
// + wayNodes.get(0).getNodeId() + " - "
// + wayNodes.get(wayNodes.size() - 1).getNodeId());
if (wayNodes.get(0).getNodeId() == lastNodeId) {
// System.out.println(" found first");
foundNext = true;
sortedWays.add(way2);
ways.remove(i);
} else if (wayNodes.get(wayNodes.size() - 1).getNodeId() == lastNodeId) {
// System.out.println(" found last");
foundNext = true;
Collections.reverse(wayNodes);
sortedWays.add(way2);
ways.remove(i);
}
}
}
if (!foundNext) {
// System.out.println("Did not find next border, adding anyways (way=" + ways.get(0).getId() + ")");
sortedWays.add(ways.get(0));
ways.remove(0);
}
}
return sortedWays;
}
/**
* Set the languages to query for, when retrieving border names.
*
* @param languages An array of language codes, such as {"en", "de"}.
*/
@Override
public void setLanguages(List languages) {
this.languages = languages;
}
/**
* Represents the role of a boundary way/relation.
*/
private static enum Role {
/**
* Inner border, thus a hole.
*/
INNER,
/**
* Outer border, thus the polygon.
*/
OUTER;
/**
* Get the role by the tag value of the tag "role".
* @param role The value of tag "role".
* @return The parsed role.
*/
@SuppressWarnings({"ConstantConditions"})
public static Role toRole(@Nullable String role) {
if (Strings.isNullOrEmpty(role)
|| role.equalsIgnoreCase("outer")
|| role.equalsIgnoreCase("exclave")) {
return OUTER;
}
return INNER;
}
}
}
borderextract-1.3/src/main/java/com/generalbytes/osmosis/borderextract/MysqlBorderListener.java 0000644 0001750 0000144 00000014171 11724635333 032522 0 ustar alison users package com.generalbytes.osmosis.borderextract;
/*
Copyright (C) 2011 by GB General Bytes GmbH, Baden, Switzerland
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
THE SOFTWARE.
*/
import java.util.logging.*;
import com.google.common.base.Strings;
import org.openstreetmap.osmosis.core.domain.v0_6.Node;
import org.openstreetmap.osmosis.core.domain.v0_6.Relation;
import org.openstreetmap.osmosis.core.domain.v0_6.Way;
import org.openstreetmap.osmosis.core.domain.v0_6.WayNode;
import org.openstreetmap.osmosis.core.store.NoSuchIndexElementException;
import java.io.FileWriter;
import java.io.IOException;
import java.io.Writer;
import java.util.ArrayList;
import java.util.List;
/**
* Create SQL code to insert the found border polygons into a mysql-spatial table.
* @author Andre Lison, GB General Bytes GmbH, 2011
*/
public class MysqlBorderListener extends KMLBorderListener {
/**
* Logger.
*/
private static final Logger LOG = Logger.getLogger(MysqlBorderListener.class.getName());
/**
* Create the sql files from polygons.
*/
@Override
public void complete(OSMData data, String outputFileName) {
LOG.info("complete");
Writer writer = null;
try {
writer = new FileWriter(outputFileName);
for (Relation relation : relations.values()) {
writeSqlForRelation(data, writer, relation);
}
} catch (IOException e) {
LOG.log(Level.SEVERE, "Error occurred", e);
} finally {
if (writer != null) {
try {
writer.close();
} catch (IOException e) {
LOG.log(Level.SEVERE, "Error occurred", e);
}
}
}
}
/**
* Write the sql for a single border-relation to the given writer.
* @param data Access to OSM-data.
* @param writer Write the SQL to this writer.
* @param relation The relation to transform and serialize.
* @throws IOException Error while writing.
*/
private void writeSqlForRelation(OSMData data, Writer writer, Relation relation) throws IOException {
String adminLevelStr = EntityHelper.getTag(relation, "admin_level");
int adminLevelInt = Strings.isNullOrEmpty(adminLevelStr) ? 0 : Integer.parseInt(adminLevelStr);
String name = EntityHelper.getName(relation, languages);
if (Strings.isNullOrEmpty(name)) {
name = "empty";
} else {
name = name.replaceAll("'", "\"");
}
try {
Polygon polygon = getPolygon(relation, data);
List ways = new ArrayList(polygon.getOuterWays().size() + polygon.getInnerWays().size());
ways.addAll(polygon.getOuterWays());
ways.addAll(polygon.getInnerWays());
writer.append("INSERT INTO Borders (Name, Admin_Level, Border) VALUES ('")
.append(name).append("', ")
.append(Integer.toString(adminLevelInt)).append(", ")
.append("GeomFromText('MULTIPOLYGON");
writer.append("("); // Multipolygon
boolean previousWay = false;
for (Way way : ways) {
if (previousWay) {
writer.append(", ");
}
writer.append("((");
boolean previousCoord = false;
Node firstNode = null;
for (WayNode wayNode : way.getWayNodes()) {
try {
Node node = data.getNode(wayNode.getNodeId());
if (!previousCoord) {
firstNode = node;
}
writeNode(writer, previousCoord, node);
previousCoord = true;
} catch (NoSuchIndexElementException e) {
LOG.warning("Can not get node " + wayNode.getNodeId());
}
}
if (firstNode != null) {
writeNode(writer, true, firstNode);
}
writer.append("))"); // polygon
previousWay = true;
}
writer.append(")"); // Multipolygon
writer.append("'));"); // GeomFromText + Values
writer.append('\n');
} catch (IncompleteBorderException e) {
LOG.log(Level.SEVERE, "Skipping border of \"" + name + "\"(" + adminLevelInt + ") due to error", e);
}
}
/**
* Write a node as polygon point to a writer.
* @param writer Write to this writer.
* @param previousCoord If there is a previous coordinate in the same polygon
* (true) or if it is the first coordinate (false).
* @param node Write this node.
* @throws IOException Error writing to the writer.
*/
private void writeNode(Writer writer, boolean previousCoord, Node node) throws IOException {
if (previousCoord) {
writer.append(", ");
}
writer.write(Double.toString(node.getLongitude()));
writer.append(' ');
writer.write(Double.toString(node.getLatitude()));
}
}
borderextract-1.3/src/main/java/com/generalbytes/osmosis/borderextract/PolyBorderListener.java 0000644 0001750 0000144 00000011400 11717402024 032317 0 ustar alison users package com.generalbytes.osmosis.borderextract;
/*
Copyright (C) 2012 by GB General Bytes GmbH, Baden, Switzerland
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
THE SOFTWARE.
*/
import com.google.common.base.Charsets;
import org.openstreetmap.osmosis.core.domain.v0_6.Node;
import org.openstreetmap.osmosis.core.domain.v0_6.Way;
import org.openstreetmap.osmosis.core.domain.v0_6.WayNode;
import org.openstreetmap.osmosis.core.store.NoSuchIndexElementException;
import javax.annotation.Nonnull;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.PrintStream;
import java.io.Writer;
import java.util.Locale;
import java.util.logging.Logger;
/**
* Border listener to export polygons to a poly-file.
* @see Poly file specification
* @author Andre Lison, GB General Bytes GmbH, 2012
*/
public class PolyBorderListener extends ZipFileBorderListener implements PolygonExporter {
/**
* Logger.
*/
@SuppressWarnings({"UnusedDeclaration"})
private static final Logger LOG = Logger.getLogger(PolyBorderListener.class.getName());
/**
* Get the export to use for marshalling a polygon.
*
* @return The exporter.
*/
@Override
public PolygonExporter getExporter() {
return this;
}
/**
* Get the file suffix to use for a single zip file entry.
*
* @return The suffix without the leading point.
*/
@Override
public String getEntrySuffix() {
return "poly";
}
/**
* Marshal a polygon to writer.
*
* @param data The full OSM data.
* @param polygon Export this polygon.
* @param writer Write marshalled polygon to this writer.
* @throws java.io.IOException Error writing to writer.
* @throws IncompleteBorderException
* Incomplete border encountered.
*/
@Override
public void writePolygon(@Nonnull OSMData data, @Nonnull Polygon polygon, @Nonnull Writer writer)
throws IOException, IncompleteBorderException {
writer.append(polygon.getName()).append('\n');
int wayCounter = 1;
for (Way outerWay : polygon.getOuterWays()) {
writeWay(data, outerWay, wayCounter, writer);
wayCounter++;
}
for (Way innerWay : polygon.getInnerWays()) {
writer.append('!');
writeWay(data, innerWay, wayCounter, writer);
wayCounter++;
}
writer.append("END").append('\n');
}
/**
* Marshal a single way to a writer.
* @param data The complete OSM data.
* @param way Marshal this way.
* @param wayNumber The number of the way.
* @param writer Marshal to this writer.
* @throws IncompleteBorderException Elements of a border are missing in OSM data.
* @throws java.io.IOException Error writing to the writer.
*/
private void writeWay(OSMData data, Way way, int wayNumber, Writer writer) throws IOException, IncompleteBorderException {
writer.append(Integer.toString(wayNumber)).append('\n');
ByteArrayOutputStream bout = new ByteArrayOutputStream();
PrintStream printStream = new PrintStream(bout);
for (WayNode wayNode : way.getWayNodes()) {
try {
Node node = data.getNode(wayNode.getNodeId());
printStream.printf(Locale.US, "\t%+E %+E\n", node.getLongitude(), node.getLatitude());
} catch(NoSuchIndexElementException e) {
throw new IncompleteBorderException("Can not get node " + wayNode.getNodeId());
}
}
printStream.close();
String coordinates = new String(bout.toByteArray(), Charsets.ISO_8859_1);
writer.append(coordinates);
bout.close();
writer.append("END\n");
}
}
borderextract-1.3/src/main/java/com/generalbytes/osmosis/borderextract/EntityHelper.java 0000644 0001750 0000144 00000007254 11725462041 031164 0 ustar alison users package com.generalbytes.osmosis.borderextract;
/*
Copyright (C) 2011, 2012 by GB General Bytes GmbH, Baden, Switzerland
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
THE SOFTWARE.
*/
import java.util.List;
import java.util.logging.Logger;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import com.google.common.base.Strings;
import org.openstreetmap.osmosis.core.domain.v0_6.Entity;
import org.openstreetmap.osmosis.core.domain.v0_6.Tag;
import javax.annotation.Nonnull;
/**
* Easy access to tags of osm-entities.
* @author Andre Lison, GB General Bytes GmbH, 2011
*/
public class EntityHelper {
/**
* Logger.
*/
@SuppressWarnings({"UnusedDeclaration"})
private static final Logger LOG = Logger.getLogger(EntityHelper.class.getName());
/**
* To detect a name tag and extract the language.
*/
private static Pattern nameTagPattern = Pattern.compile("name(:([a-zA-Z]{2,3}))?");
/**
* Tag "name".
*/
private static final String NAME_TAG = "name";
/**
* Get the value of a tag by key.
* @return the tag-value for the given key
* @param node the way to get the tag from
* @param key the key of the tag
*/
public static String getTag(final Entity node, final String key) {
for (Tag tag : node.getTags()) {
if (tag.getKey().equals(key)) {
return tag.getValue();
}
}
return null;
}
/**
* Get the name of the given entity in the desired language(s) or in
* the default language, if no other language matches.
* @param entity Get the name of this entity.
* @param languages Desired languages. Can be null.
* @return The name or null, if none was found.
*/
public static String getName(@Nonnull final Entity entity, final List languages) {
String bestMatch = getTag(entity, NAME_TAG);
int bestRank = Integer.MAX_VALUE;
for (Tag tag : entity.getTags()) {
Matcher nameMatcher = nameTagPattern.matcher(tag.getKey());
if (nameMatcher.matches()) {
if (Strings.isNullOrEmpty(bestMatch)) {
bestMatch = tag.getValue();
}
String lang = Strings.nullToEmpty(nameMatcher.group(2));
for (int i = 0; languages != null && i < languages.size(); i++) {
if (lang.equalsIgnoreCase(languages.get(i)) && bestRank > i) {
bestMatch = tag.getValue();
bestRank = i;
}
}
}
}
return Strings.isNullOrEmpty(bestMatch)
? getTag(entity, "int_name")
: bestMatch;
}
}
borderextract-1.3/src/main/java/com/generalbytes/osmosis/borderextract/BorderListener.java 0000644 0001750 0000144 00000004223 11724635333 031471 0 ustar alison users package com.generalbytes.osmosis.borderextract;
/*
Copyright (C) 2011, 2012 by GB General Bytes GmbH, Baden, Switzerland
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
THE SOFTWARE.
*/
import org.openstreetmap.osmosis.core.domain.v0_6.Relation;
import java.util.List;
/**
* Listens for border relations.
* @author Andre Lison, GB General Bytes GmbH, 2011
*/
public interface BorderListener {
/**
* Called, when a relation was found that is an administrative border.
* @param relation The found relation.
* @param adminLevel The admin level of the border.
* @return true, if subsequent listeners should be called or if processing is stopped.
*/
boolean borderRelationFound(Relation relation, int adminLevel);
/**
* Signals the listener, that processing is complete.
* @param data Provides access to osm data.
* @param outputFileName Write result to this file.
*/
void complete(OSMData data, String outputFileName);
/**
* Set the languages to query for, when retrieving border names.
* @param languages An array of language codes, such as {"en", "de"}.
*/
void setLanguages(List languages);
}
borderextract-1.3/src/main/java/com/generalbytes/osmosis/borderextract/BorderExtractTaskFactory.java0000644 0001750 0000144 00000013116 11724635333 033472 0 ustar alison users package com.generalbytes.osmosis.borderextract;
/*
Copyright (C) 2011 by GB General Bytes GmbH, Baden, Switzerland
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
THE SOFTWARE.
*/
import com.google.common.annotations.VisibleForTesting;
import com.google.common.base.Splitter;
import com.google.common.base.Strings;
import com.google.common.collect.Lists;
import org.openstreetmap.osmosis.core.pipeline.common.TaskConfiguration;
import org.openstreetmap.osmosis.core.pipeline.common.TaskManager;
import org.openstreetmap.osmosis.core.pipeline.common.TaskManagerFactory;
import org.openstreetmap.osmosis.core.pipeline.v0_6.SinkManager;
import java.lang.reflect.Constructor;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.logging.Level;
import java.util.logging.Logger;
/**
* Parse command line arguments and initialize the plugin.
* @author Andre Lison, GB General Bytes GmbH
*/
public class BorderExtractTaskFactory extends TaskManagerFactory {
/**
* Logger.
*/
@SuppressWarnings({"UnusedDeclaration"})
private static final Logger LOG = Logger.getLogger(BorderExtractTaskFactory.class.getName());
private static final String ARG_OUTPUT_TYPE = "type";
private static final String ARG_OUTPUT_FILE = "file";
private static final String ARG_LEVELS = "levels";
private static final String ARG_LANGUAGE = "lang";
private static final int MIN_LEVEL = 0;
private static final int MAX_LEVEL = 11;
@Override
protected TaskManager createTaskManagerImpl(TaskConfiguration taskConfig) {
Map listenerMap = new HashMap();
listenerMap.put("kml", new Object[]{KMLBorderListener.class, "zip"});
listenerMap.put("mysql", new Object[]{MysqlBorderListener.class, "sql"});
listenerMap.put("poly", new Object[]{PolyBorderListener.class, "zip"});
// output type
String outputType = getStringArgument(taskConfig, ARG_OUTPUT_TYPE, "kml");
Object[] listenerConf = listenerMap.get(outputType);
if (listenerConf == null) {
LOG.severe("Unsupported output type \"" + outputType + "\" found. Aborting.");
return null;
}
// parse levels
String levelsString = getStringArgument(taskConfig, ARG_LEVELS, "");
boolean[] levels = parseLevels(levelsString);
// output file name
String outputFileName = getStringArgument(taskConfig, ARG_OUTPUT_FILE, "borderextract." + listenerConf[1]);
// language
String langs = getStringArgument(taskConfig, ARG_LANGUAGE, "");
Splitter langSplitter = Splitter.on(",").omitEmptyStrings().trimResults();
ArrayList languages = Lists.newArrayList(langSplitter.split(langs));
// create the task
try {
BorderExtractTask task = new BorderExtractTask(outputFileName, levels);
BorderListener listener = (BorderListener) ((Class) listenerConf[0]).newInstance();
listener.setLanguages(languages);
task.addBorderListener(listener);
return new SinkManager(taskConfig.getId(),
task,
taskConfig.getPipeArgs());
} catch (Exception e) {
LOG.log(Level.SEVERE, "Unable to instantiate the \"borderextract\" plugin. Error occurred", e);
}
return null;
}
/**
* Extract the configured levels from the string.
* @param levelsString The string as given by the user on the command line.
* @return An array of boolean containing a true at each index that is a recognized admin level.
*/
@SuppressWarnings({"PointlessArithmeticExpression"})
@VisibleForTesting
protected boolean[] parseLevels(String levelsString) {
boolean[] levels = new boolean[MAX_LEVEL - MIN_LEVEL + 1];
if (!Strings.isNullOrEmpty(levelsString)) {
Iterable levelList = Splitter.on(",").omitEmptyStrings().trimResults().split(levelsString);
for (String level : levelList) {
try {
int parsedLevel = Integer.parseInt(level);
if (parsedLevel < MIN_LEVEL || parsedLevel > MAX_LEVEL) {
LOG.warning("Admin level exceeds range: " + parsedLevel
+ ". Allowed values: " + MIN_LEVEL + " to " + MAX_LEVEL);
} else {
levels[parsedLevel] = true;
}
} catch (NumberFormatException nfe) {
LOG.warning("Can not parse level \"" + level + "\". Ignoring.");
}
}
}
return levels;
}
}
borderextract-1.3/src/main/java/com/generalbytes/osmosis/borderextract/ZipFileBorderListener.java 0000644 0001750 0000144 00000010144 11724635333 032753 0 ustar alison users package com.generalbytes.osmosis.borderextract;
/*
Copyright (C) 2011 by GB General Bytes GmbH, Baden, Switzerland
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
THE SOFTWARE.
*/
import org.openstreetmap.osmosis.core.domain.v0_6.Relation;
import java.io.BufferedOutputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.OutputStreamWriter;
import java.util.logging.Level;
import java.util.logging.Logger;
import java.util.zip.ZipEntry;
import java.util.zip.ZipOutputStream;
/**
* Marshal multiple polygons into a single zip file.
* @author Andre Lison, GB General Bytes GmbH, 2012
*/
public abstract class ZipFileBorderListener extends BaseBorderListener {
/**
* Logger.
*/
@SuppressWarnings({"UnusedDeclaration"})
private static final Logger LOG = Logger.getLogger(ZipFileBorderListener.class.getName());
/**
* Marshal collected border data to the output file, which is a zip file.
*/
@Override
public void complete(OSMData data, String outputFileName) {
LOG.info("complete");
ZipOutputStream zout = null;
try {
zout = new ZipOutputStream(new FileOutputStream(outputFileName));
for (Relation relation : relations.values()) {
writeRelation(data, zout, relation);
}
} catch (IOException e) {
LOG.log(Level.SEVERE, "Error writing output file", e);
} finally {
if (zout != null) {
try {
zout.close();
} catch (IOException e) {
LOG.log(Level.SEVERE, "Error occurred", e);
}
}
}
}
/**
* Marshal a single relation.
*
* @param data Access to OSM-Data.
* @param zout Write to this zip file.
* @param relation The relation to serialize.
* @throws java.io.IOException Error writing to zip file.
*/
void writeRelation(OSMData data, ZipOutputStream zout, Relation relation) throws IOException {
try {
Polygon polygon = getPolygon(relation, data);
// create zip entry
String zipEntryName = polygon.getName().replaceAll("[\\\\/]", "-")
+ "_" + relation.getId()
+ "." + getEntrySuffix();
ZipEntry zipEntry = new ZipEntry(zipEntryName);
zout.putNextEntry(zipEntry);
OutputStreamWriter writer = new OutputStreamWriter(new BufferedOutputStream(zout));
// Export the polygon.
getExporter().writePolygon(data, polygon, writer);
writer.flush();
zout.closeEntry();
} catch (IncompleteBorderException e) {
String name = EntityHelper.getName(relation, languages);
LOG.log(Level.SEVERE, "Skipping border of \"" + name + "\" due to error", e);
}
}
/**
* Get the export to use for marshalling a polygon.
* @return The exporter.
*/
public abstract PolygonExporter getExporter();
/**
* Get the file suffix to use for a single zip file entry.
* @return The suffix without the leading point.
*/
public abstract String getEntrySuffix();
} borderextract-1.3/src/main/sql/ 0000755 0001750 0000144 00000000000 11732070445 015746 5 ustar alison users borderextract-1.3/src/main/sql/setup_mysql.sql 0000644 0001750 0000144 00000000215 11677047757 021074 0 ustar alison users CREATE TABLE Borders (
Name VARCHAR(255),
Admin_Level TINYINT NOT NULL,
Border GEOMETRY NOT NULL,
SPATIAL INDEX(Border)
) ENGINE=MyISAM;
borderextract-1.3/src/main/resources/ 0000755 0001750 0000144 00000000000 11732070445 017161 5 ustar alison users borderextract-1.3/src/main/resources/osmosis-plugins.conf 0000644 0001750 0000144 00000000072 11666461342 023210 0 ustar alison users com.generalbytes.osmosis.borderextract.BorderExtractPlugin borderextract-1.3/src/test/ 0000755 0001750 0000144 00000000000 11732070445 015202 5 ustar alison users borderextract-1.3/src/test/java/ 0000755 0001750 0000144 00000000000 11732070445 016123 5 ustar alison users borderextract-1.3/src/test/java/com/ 0000755 0001750 0000144 00000000000 11732070445 016701 5 ustar alison users borderextract-1.3/src/test/java/com/generalbytes/ 0000755 0001750 0000144 00000000000 11732070445 021365 5 ustar alison users borderextract-1.3/src/test/java/com/generalbytes/osmosis/ 0000755 0001750 0000144 00000000000 11732070445 023061 5 ustar alison users borderextract-1.3/src/test/java/com/generalbytes/osmosis/borderextract/ 0000755 0001750 0000144 00000000000 11732070445 025731 5 ustar alison users borderextract-1.3/src/test/java/com/generalbytes/osmosis/borderextract/EntityHelperTest.java 0000644 0001750 0000144 00000011510 11725462041 032045 0 ustar alison users package com.generalbytes.osmosis.borderextract;
/*
Copyright (C) 2012 by GB General Bytes GmbH, Baden, Switzerland
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
THE SOFTWARE.
*/
import com.google.common.base.Strings;
import com.google.common.collect.Collections2;
import com.google.common.collect.Lists;
import org.apache.log4j.Logger;
import org.junit.Assert;
import org.junit.Test;
import org.openstreetmap.osmosis.core.domain.v0_6.Entity;
import org.openstreetmap.osmosis.core.domain.v0_6.Node;
import org.openstreetmap.osmosis.core.domain.v0_6.OsmUser;
import org.openstreetmap.osmosis.core.domain.v0_6.Tag;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
/**
* Test class {@link EntityHelper}.
* @author Andre Lison, GB General Bytes GmbH, 2012
*/
public class EntityHelperTest {
/**
* Logger.
*/
@SuppressWarnings({"UnusedDeclaration"})
private static final Logger LOG = Logger.getLogger(EntityHelperTest.class.getSimpleName());
/**
* Test method {@link ${SIMPLE_CLASS_NAME}#GetName()}.
*
* @throws Exception Test failed.
*/
@Test
public void testGetName() throws Exception {
Entity entity = new Node(1, 1, new Date(), OsmUser.NONE, 1, 1.0, 2.0);
// no name, no desired lang
Assert.assertTrue(Strings.isNullOrEmpty(EntityHelper.getName(entity, null)));
// default name, no desired lang
entity.getTags().add(new Tag("name", "München"));
Assert.assertEquals("München", EntityHelper.getName(entity, null));
// german name, no desired lang
entity.getTags().clear();
entity.getTags().add(new Tag("name:de", "München"));
Assert.assertEquals("München", EntityHelper.getName(entity, null));
// german name, desired lang is english
List langs = Lists.newArrayList("en");
Assert.assertEquals("München", EntityHelper.getName(entity, langs));
// german + english name, desired lang is english
entity.getTags().add(new Tag("name:en", "Munich"));
Assert.assertEquals("Munich", EntityHelper.getName(entity, langs));
// german + english name, desired lang is german and then english
langs = Lists.newArrayList("de", "en");
Assert.assertEquals("München", EntityHelper.getName(entity, langs));
// german + english name, desired lang is english and then german
langs = Lists.newArrayList("en", "de");
Assert.assertEquals("Munich", EntityHelper.getName(entity, langs));
// german + english name, desired lang is russian
langs = Lists.newArrayList("ru");
Assert.assertEquals("München", EntityHelper.getName(entity, langs));
// german + english + latin name, desired lang is russian, then latin
langs = Lists.newArrayList("ru", "la");
entity.getTags().add(new Tag("name:la", "Monachium"));
Assert.assertEquals("Monachium", EntityHelper.getName(entity, langs));
// no name at all, but international name
langs = Lists.newArrayList("ru", "la");
entity.getTags().clear();
entity.getTags().add(new Tag("int_name", "Munich"));
Assert.assertEquals("Munich", EntityHelper.getName(entity, langs));
// only default name, desired lang russian and latin
entity.getTags().add(new Tag("name", "München"));
Assert.assertEquals("München", EntityHelper.getName(entity, langs));
// ISO 639-2 Code "bad", "Banda languages", desired lang russian and latin
entity.getTags().add(new Tag("name", "?"));
langs = Lists.newArrayList("ru", "la");
Assert.assertEquals("München", EntityHelper.getName(entity, langs));
// ISO 639-2 Code "bad", "Banda languages", desired lang "bad"
entity.getTags().add(new Tag("name:bad", "?"));
langs = Lists.newArrayList("bad");
Assert.assertEquals("?", EntityHelper.getName(entity, langs));
}
}
borderextract-1.3/src/test/java/com/generalbytes/osmosis/borderextract/BaseBorderListenerTest.java 0000644 0001750 0000144 00000021262 11724635333 033161 0 ustar alison users package com.generalbytes.osmosis.borderextract;
/*
Copyright (C) 2011 by GB General Bytes GmbH, Baden, Switzerland
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
THE SOFTWARE.
*/
import org.apache.log4j.Logger;
import org.junit.AfterClass;
import org.junit.Assert;
import org.junit.BeforeClass;
import org.junit.Test;
import org.openstreetmap.osmosis.core.domain.v0_6.EntityType;
import org.openstreetmap.osmosis.core.domain.v0_6.Node;
import org.openstreetmap.osmosis.core.domain.v0_6.OsmUser;
import org.openstreetmap.osmosis.core.domain.v0_6.Relation;
import org.openstreetmap.osmosis.core.domain.v0_6.RelationMember;
import org.openstreetmap.osmosis.core.domain.v0_6.Tag;
import org.openstreetmap.osmosis.core.domain.v0_6.Way;
import org.openstreetmap.osmosis.core.store.IndexedObjectStoreReader;
import javax.management.relation.RelationNotFoundException;
import java.util.Date;
/**
* Test class {@link KMLBorderListener}.
* @author Andre Lison, GB General Bytes GmbH, 2011
*/
public class BaseBorderListenerTest extends BaseBorderListener {
/**
* Logger.
*/
@SuppressWarnings({"UnusedDeclaration"})
private static final Logger LOG = Logger.getLogger(BaseBorderListenerTest.class.getName());
/**
* Holds test data.
*/
private static TestDataProvider dataProvider;
@BeforeClass
public static void setUp() throws Exception {
dataProvider = new TestDataProvider();
}
@AfterClass
public static void tearDown() throws Exception {
dataProvider.deleteTestData();
}
/**
* Test method {@link KMLBorderListener#borderRelationFound(Relation, int)}
*
* @throws Exception Test failed.
*/
@Test
public void testBorderRelationFound() throws Exception {
Relation relation = new Relation(1, 1, new Date(), OsmUser.NONE, 1);
relation.getTags().add(new Tag("name", "test name"));
// --------------------------------------------------------------------------
// big outer ring with two inner rings
relation.getMembers().add(new RelationMember(1, EntityType.Way, "outer"));
relation.getMembers().add(new RelationMember(2, EntityType.Way, "outer"));
relation.getMembers().add(new RelationMember(3, EntityType.Way, "inner"));
relation.getMembers().add(new RelationMember(4, EntityType.Way, "inner"));
relation.getMembers().add(new RelationMember(5, EntityType.Way, "outer")); // empty
IndexedObjectStoreReader nodeStoreReader = dataProvider.getNodeStore().createReader();
IndexedObjectStoreReader wayStoreReader = dataProvider.getWayStore().createReader();
OSMData osmData = new OSMData(nodeStoreReader, wayStoreReader);
// test 1
boolean c = this.borderRelationFound(relation, 8);
Assert.assertEquals(1, relations.size());
Polygon p = getPolygon(relation, osmData);
Assert.assertTrue(c);
Assert.assertEquals("test name", p.getName());
Assert.assertNotNull(p.getInnerWays());
Assert.assertNotNull(p.getOuterWays());
Assert.assertEquals(1, p.getOuterWays().size());
Assert.assertEquals(2, p.getInnerWays().size());
Assert.assertEquals(7, p.getOuterWays().get(0).getWayNodes().size());
Assert.assertEquals(4, p.getInnerWays().get(0).getWayNodes().size());
Assert.assertEquals(4, p.getInnerWays().get(1).getWayNodes().size());
// --------------------------------------------------------------------------
// test with nested relations
Relation parentRelation = new Relation(100, 1, new Date(), OsmUser.NONE, 1);
parentRelation.getMembers().add(new RelationMember(1, EntityType.Relation, "outer"));
parentRelation.getTags().add(new Tag("name", "parent relation"));
p = getPolygon(parentRelation, osmData);
Assert.assertEquals("parent relation", p.getName());
Assert.assertNotNull(p.getInnerWays());
Assert.assertNotNull(p.getOuterWays());
Assert.assertEquals(1, p.getOuterWays().size());
Assert.assertEquals(2, p.getInnerWays().size());
Assert.assertEquals(7, p.getOuterWays().get(0).getWayNodes().size());
Assert.assertEquals(4, p.getInnerWays().get(0).getWayNodes().size());
Assert.assertEquals(4, p.getInnerWays().get(1).getWayNodes().size());
this.complete(osmData, "junit-output");
// --------------------------------------------------------------------------
// test with non-existing way
relation.getMembers().add(new RelationMember(7, EntityType.Way, "outer")); // does not exist!
relations.clear();
try {
getPolygon(relation, osmData);
Assert.fail("Exception expected");
} catch (IncompleteBorderException e) {
}
// --------------------------------------------------------------------------
// test with ways in the wrong order
relations.clear();
relation.getMembers().clear();
relation.getMembers().add(new RelationMember(2, EntityType.Way, "outer")); // 2 before 1!
relation.getMembers().add(new RelationMember(1, EntityType.Way, "outer"));
relation.getMembers().add(new RelationMember(3, EntityType.Way, "inner"));
relation.getMembers().add(new RelationMember(4, EntityType.Way, "inner"));
relation.getMembers().add(new RelationMember(5, EntityType.Way, "outer")); // empty
this.complete(osmData, "junit-output");
// test 2
c = this.borderRelationFound(relation, 8);
Assert.assertEquals(1, relations.size());
p = getPolygon(relation, osmData);
Assert.assertTrue(c);
Assert.assertEquals("test name", p.getName());
Assert.assertNotNull(p.getInnerWays());
Assert.assertNotNull(p.getOuterWays());
Assert.assertEquals(1, p.getOuterWays().size());
Assert.assertEquals(2, p.getInnerWays().size());
Assert.assertEquals(7, p.getOuterWays().get(0).getWayNodes().size());
Assert.assertEquals(4, p.getInnerWays().get(0).getWayNodes().size());
Assert.assertEquals(4, p.getInnerWays().get(1).getWayNodes().size());
// --------------------------------------------------------------------------
// test with way2 reversed
relations.clear();
relation.getMembers().clear();
relation.getMembers().add(new RelationMember(1, EntityType.Way, "outer"));
relation.getMembers().add(new RelationMember(6, EntityType.Way, "outer")); // reversed node order!
relation.getMembers().add(new RelationMember(3, EntityType.Way, "inner"));
relation.getMembers().add(new RelationMember(4, EntityType.Way, "inner"));
relation.getMembers().add(new RelationMember(5, EntityType.Way, "outer")); // empty
this.complete(osmData, "junit-output");
// test 3
c = this.borderRelationFound(relation, 8);
Assert.assertEquals(1, relations.size());
p = getPolygon(relation, osmData);
Assert.assertTrue(c);
Assert.assertEquals("test name", p.getName());
Assert.assertNotNull(p.getInnerWays());
Assert.assertNotNull(p.getOuterWays());
Assert.assertEquals(1, p.getOuterWays().size());
Assert.assertEquals(2, p.getInnerWays().size());
Assert.assertEquals(7, p.getOuterWays().get(0).getWayNodes().size());
Assert.assertEquals(4, p.getInnerWays().get(0).getWayNodes().size());
Assert.assertEquals(4, p.getInnerWays().get(1).getWayNodes().size());
// cleanup
nodeStoreReader.release();
wayStoreReader.release();
}
/**
* Signals the listener, that processing is complete.
*
* @param data Provides access to osm data.
*/
@Override
public void complete(OSMData data, String outputFileName) {
}
}
borderextract-1.3/src/test/java/com/generalbytes/osmosis/borderextract/TestDataProvider.java 0000644 0001750 0000144 00000015110 11724635333 032022 0 ustar alison users package com.generalbytes.osmosis.borderextract;
/*
Copyright (C) 2011 by GB General Bytes GmbH, Baden, Switzerland
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
THE SOFTWARE.
*/
import org.apache.log4j.Logger;
import org.junit.Ignore;
import org.openstreetmap.osmosis.core.domain.v0_6.EntityType;
import org.openstreetmap.osmosis.core.domain.v0_6.Node;
import org.openstreetmap.osmosis.core.domain.v0_6.OsmUser;
import org.openstreetmap.osmosis.core.domain.v0_6.Relation;
import org.openstreetmap.osmosis.core.domain.v0_6.RelationMember;
import org.openstreetmap.osmosis.core.domain.v0_6.Tag;
import org.openstreetmap.osmosis.core.domain.v0_6.Way;
import org.openstreetmap.osmosis.core.domain.v0_6.WayNode;
import org.openstreetmap.osmosis.core.store.IndexedObjectStore;
import org.openstreetmap.osmosis.core.store.SingleClassObjectSerializationFactory;
import java.util.Collections;
import java.util.Date;
/**
* Create some test data and provide it to test classes.
* @author Andre Lison, GB General Bytes GmbH, 2011
*/
@Ignore
public class TestDataProvider {
/**
* Logger.
*/
@SuppressWarnings({"UnusedDeclaration"})
private static final Logger LOG = Logger.getLogger(TestDataProvider.class.getName());
/**
* The node store with all nodes.
*/
private IndexedObjectStore nodeStore;
/**
* The way store containing all ways.
*/
private IndexedObjectStore wayStore;
/**
* A single border relation.
*/
private Relation relation;
public TestDataProvider() {
createTestData();
}
/**
* Create the test data.
*/
private void createTestData() {
// preparation
nodeStore = new IndexedObjectStore(
new SingleClassObjectSerializationFactory(Node.class), "junit-nodes");
wayStore = new IndexedObjectStore(
new SingleClassObjectSerializationFactory(Way.class), "junit-ways");
double[] nodeCoords = {
47.482829, 8.295433,
47.484622, 8.311698,
47.477734, 8.321771,
47.464903, 8.320621,
47.462052, 8.297045,
47.469074, 8.284794,
47.474237, 8.278815,
47.476120, 8.284164,
47.474061, 8.288497,
47.472109, 8.284217,
47.479688, 8.302621,
47.479861, 8.306419,
47.477824, 8.306670,
47.478041, 8.303481
};
for (int i = 0; i < nodeCoords.length; i +=2) {
nodeStore.add(i / 2 + 1, new Node(i / 2 + 1, 1, new Date(), OsmUser.NONE, 1, nodeCoords[i], nodeCoords[i + 1]));
}
Way w1 = new Way(1, 1, new Date(), OsmUser.NONE, 1);
w1.getWayNodes().add(new WayNode(1));
w1.getWayNodes().add(new WayNode(2));
w1.getWayNodes().add(new WayNode(3));
w1.getWayNodes().add(new WayNode(4));
Way w2 = new Way(2, 1, new Date(), OsmUser.NONE, 1);
w2.getWayNodes().add(new WayNode(4));
w2.getWayNodes().add(new WayNode(5));
w2.getWayNodes().add(new WayNode(6));
w2.getWayNodes().add(new WayNode(7));
w2.getWayNodes().add(new WayNode(1));
Way w3 = new Way(3, 1, new Date(), OsmUser.NONE, 1);
w3.getWayNodes().add(new WayNode(7));
w3.getWayNodes().add(new WayNode(8));
w3.getWayNodes().add(new WayNode(9));
w3.getWayNodes().add(new WayNode(10));
Way w4 = new Way(4, 1, new Date(), OsmUser.NONE, 1);
w4.getWayNodes().add(new WayNode(11));
w4.getWayNodes().add(new WayNode(12));
w4.getWayNodes().add(new WayNode(13));
w4.getWayNodes().add(new WayNode(14));
Way w5 = new Way(5, 1, new Date(), OsmUser.NONE, 1);
Way w2rev = new Way(6, 1, new Date(), OsmUser.NONE, 1);
w2rev.getWayNodes().addAll(w2.getWayNodes());
Collections.reverse(w2rev.getWayNodes());
wayStore.add(1, w1);
wayStore.add(2, w2);
wayStore.add(3, w3);
wayStore.add(4, w4);
wayStore.add(5, w5);
wayStore.add(6, w2rev);
nodeStore.complete();
wayStore.complete();
// create a border relation
relation = new Relation(1, 1, new Date(), OsmUser.NONE, 1);
relation.getTags().add(new Tag("name", "test name"));
// --------------------------------------------------------------------------
// big outer ring with two inner rings
relation.getMembers().add(new RelationMember(1, EntityType.Way, "outer"));
relation.getMembers().add(new RelationMember(2, EntityType.Way, "outer"));
relation.getMembers().add(new RelationMember(3, EntityType.Way, "inner"));
relation.getMembers().add(new RelationMember(4, EntityType.Way, "inner"));
relation.getMembers().add(new RelationMember(5, EntityType.Way, "outer")); // empty
relation.getMembers().add(new RelationMember(7, EntityType.Way, "outer")); // does not exist!
}
/**
* Close all stores and thus delete the test data.
*/
public void deleteTestData() {
if (nodeStore != null) {
nodeStore.release();
nodeStore = null;
}
if (wayStore != null) {
wayStore.release();
wayStore = null;
}
}
public IndexedObjectStore getNodeStore() {
return nodeStore;
}
public IndexedObjectStore getWayStore() {
return wayStore;
}
public void finalize() {
deleteTestData();
}
/**
* Get the test border relation.
* @return The relation.
*/
public Relation getRelation() {
return relation;
}
}
borderextract-1.3/src/test/java/com/generalbytes/osmosis/borderextract/MysqlBorderListenerTest.java 0000644 0001750 0000144 00000006335 11717401124 033407 0 ustar alison users package com.generalbytes.osmosis.borderextract;
/*
Copyright (C) 2011 by GB General Bytes GmbH, Baden, Switzerland
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
THE SOFTWARE.
*/
import org.apache.log4j.Logger;
import org.junit.AfterClass;
import org.junit.Assert;
import org.junit.BeforeClass;
import org.junit.Test;
import org.openstreetmap.osmosis.core.domain.v0_6.EntityType;
import org.openstreetmap.osmosis.core.domain.v0_6.Node;
import org.openstreetmap.osmosis.core.domain.v0_6.OsmUser;
import org.openstreetmap.osmosis.core.domain.v0_6.Relation;
import org.openstreetmap.osmosis.core.domain.v0_6.RelationMember;
import org.openstreetmap.osmosis.core.domain.v0_6.Tag;
import org.openstreetmap.osmosis.core.domain.v0_6.Way;
import org.openstreetmap.osmosis.core.store.IndexedObjectStoreReader;
import java.util.Date;
/**
* Test class {@link MysqlBorderListener}.
* @author Andre Lison, GB General Bytes GmbH, 2011
*/
public class MysqlBorderListenerTest {
/**
* Logger.
*/
@SuppressWarnings({"UnusedDeclaration"})
private static final Logger LOG = Logger.getLogger(MysqlBorderListenerTest.class.getName());
/**
* Holds test data.
*/
private static TestDataProvider dataProvider;
@BeforeClass
public static void setUp() throws Exception {
dataProvider = new TestDataProvider();
}
@AfterClass
public static void tearDown() throws Exception {
dataProvider.deleteTestData();
}
/**
* Test method {@link MysqlBorderListener#complete(OSMData, String)}.
*
* @throws Exception Test failed.
*/
@Test
public void testComplete() throws Exception {
Relation relation = dataProvider.getRelation();
IndexedObjectStoreReader nodeStoreReader = dataProvider.getNodeStore().createReader();
IndexedObjectStoreReader wayStoreReader = dataProvider.getWayStore().createReader();
OSMData osmData = new OSMData(nodeStoreReader, wayStoreReader);
// test 1
MysqlBorderListener listener = new MysqlBorderListener();
boolean c = listener.borderRelationFound(relation, 8);
listener.complete(osmData, "junit-kml-out");
Assert.assertTrue(true);
// cleanup
nodeStoreReader.release();
wayStoreReader.release();
}
}
././@LongLink 0000000 0000000 0000000 00000000151 00000000000 011562 L ustar root root borderextract-1.3/src/test/java/com/generalbytes/osmosis/borderextract/BorderExtractTaskFactoryTest.java borderextract-1.3/src/test/java/com/generalbytes/osmosis/borderextract/BorderExtractTaskFactoryTest.0000644 0001750 0000144 00000006436 11717401124 033521 0 ustar alison users package com.generalbytes.osmosis.borderextract;
/*
Copyright (C) 2011 by GB General Bytes GmbH, Baden, Switzerland
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
THE SOFTWARE.
*/
import org.apache.log4j.Logger;
import org.junit.Assert;
import org.junit.Test;
/**
* Test class {@link BorderExtractTaskFactory}.
* @author Andre Lison, GB General Bytes GmbH, 2011
*/
public class BorderExtractTaskFactoryTest extends BorderExtractTaskFactory {
/**
* Logger.
*/
@SuppressWarnings({"UnusedDeclaration"})
private static final Logger LOG = Logger.getLogger(BorderExtractTaskFactoryTest.class.getName());
/**
* Test method {@link BorderExtractTaskFactory#parseLevels(String)}.
*/
@Test
public void testParseLevels() {
Assert.assertArrayEquals(
conv(new boolean[]{false, false, false, false, false, false, false, false, false, false, false, false}),
conv(parseLevels(""))
);
Assert.assertArrayEquals(
conv(new boolean[]{false, false, false, false, false, false, false, false, false, false, false, false}),
conv(parseLevels("a"))
);
Assert.assertArrayEquals(
conv(new boolean[]{false, true, false, false, false, false, false, false, false, false, false, false}),
conv(parseLevels("1"))
);
Assert.assertArrayEquals(
conv(new boolean[]{false, true, true, false, false, false, false, false, false, false, false, false}),
conv(parseLevels("1,2"))
);
Assert.assertArrayEquals(
conv(new boolean[]{false, true, true, false, false, false, false, false, false, false, false, false}),
conv(parseLevels("1,2, 100"))
);
Assert.assertArrayEquals(
conv(new boolean[]{false, true, true, false, false, true, false, false, false, false, false, false}),
conv(parseLevels("1,2,...,5,100"))
);
}
/**
* Convert a boolean array to an integer array in order to pass it to assertArrayEquals.
* Why is it missing in junit?
*/
private int[] conv(boolean [] b) {
int[] i = new int[b.length];
for (int j = 0; j < b.length; j++) {
i[j] = b[j] ? 1 : 0;
}
return i;
}
}
borderextract-1.3/src/test/java/com/generalbytes/osmosis/borderextract/PolyBorderListenerTest.java 0000644 0001750 0000144 00000010637 11717401124 033225 0 ustar alison users package com.generalbytes.osmosis.borderextract;
/*
Copyright (C) 2012 by GB General Bytes GmbH, Baden, Switzerland
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
THE SOFTWARE.
*/
import org.apache.log4j.Logger;
import org.junit.Assert;
import org.junit.Test;
import org.openstreetmap.osmosis.core.domain.v0_6.Node;
import org.openstreetmap.osmosis.core.domain.v0_6.Relation;
import org.openstreetmap.osmosis.core.domain.v0_6.Way;
import org.openstreetmap.osmosis.core.store.IndexedObjectStoreReader;
import java.io.StringWriter;
/**
* Test {@link PolyBorderListener}.
* @author Andre Lison, GB General Bytes GmbH, 2012
*/
public class PolyBorderListenerTest {
/**
* Logger.
*/
@SuppressWarnings({"UnusedDeclaration"})
private static final Logger LOG = Logger.getLogger(PolyBorderListenerTest.class.getName());
/**
* Test method {@link PolyBorderListener#writePolygon(OSMData, Polygon, java.io.Writer)}.
* @throws Exception Test failed.
*/
@Test
public void testWritePolygon() throws Exception {
TestDataProvider testDataProvider = new TestDataProvider();
IndexedObjectStoreReader nodesReader = testDataProvider.getNodeStore().createReader();
IndexedObjectStoreReader waysReader = testDataProvider.getWayStore().createReader();
try {
// Preparation
OSMData osmData = new OSMData(nodesReader, waysReader);
BaseBorderListener baseBorderListener = new BaseBorderListener() {
@Override
public void complete(OSMData data, String outputFileName) {
}
};
PolyBorderListener listener = new PolyBorderListener();
Relation relation = testDataProvider.getRelation();
Polygon polygon;
relation.getMembers().remove(5); // remove non-existing way-member
polygon = baseBorderListener.getPolygon(relation, osmData);
StringWriter writer = new StringWriter();
// test execution
listener.writePolygon(osmData, polygon, writer);
writer.close();
// verify
String expected =
"test name\n" +
"1\n" +
"\t+8.311698E+00 +4.748462E+01\n" +
"\t+8.321771E+00 +4.747773E+01\n" +
"\t+8.320621E+00 +4.746490E+01\n" +
"\t+8.297045E+00 +4.746205E+01\n" +
"\t+8.284794E+00 +4.746907E+01\n" +
"\t+8.278815E+00 +4.747424E+01\n" +
"\t+8.295433E+00 +4.748283E+01\n" +
"END\n" +
"!2\n" +
"\t+8.278815E+00 +4.747424E+01\n" +
"\t+8.284164E+00 +4.747612E+01\n" +
"\t+8.288497E+00 +4.747406E+01\n" +
"\t+8.284217E+00 +4.747211E+01\n" +
"END\n" +
"!3\n" +
"\t+8.302621E+00 +4.747969E+01\n" +
"\t+8.306419E+00 +4.747986E+01\n" +
"\t+8.306670E+00 +4.747782E+01\n" +
"\t+8.303481E+00 +4.747804E+01\n" +
"END\n" +
"END\n";
Assert.assertEquals(expected, writer.toString());
} finally {
nodesReader.release();
waysReader.release();
testDataProvider.getNodeStore().release();
testDataProvider.getWayStore().release();
}
}
}
borderextract-1.3/LICENSE.txt 0000644 0001750 0000144 00000002062 11702537303 015255 0 ustar alison users Copyright (C) 2011 by GB General Bytes GmbH, Baden, Switzerland
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
borderextract-1.3/pom.xml 0000644 0001750 0000144 00000010551 11725462041 014752 0 ustar alison users 4.0.0com.generalbytesborderextractjarborderextract1.3Openstreetmap osmosis plugin to extract borders from osm.GB General Bytes GmbHhttp://www.general-bytes.comMETAndré LisonGB General Bytes GmbHhttp://www.general-bytes.comMIT LicenseLICENSE.txtmanuallymaven-compiler-plugin1.61.6UTF-8truemaven-resources-pluginUTF-8com.google.guavaguava10.0.1org.openstreetmap.osmosisosmosis-core0.39checkstylecheckstyle4.4testjunitjunit4.8.1testnet.sourceforge.coberturacobertura1.9.4testorg.codehaus.mojocobertura-maven-pluginorg.apache.maven.pluginsmaven-checkstyle-pluginorg.apache.maven.pluginsmaven-pmd-plugintrueutf-81.6org.apache.maven.pluginsmaven-javadoc-plugin
http://java.sun.com/javase/6/docs/api/
en_USprivatemaven2maven2http://repo2.maven.org/maven2