borderextract-1.3/0000755000175000001440000000000011732070445013434 5ustar alisonusersborderextract-1.3/src/0000755000175000001440000000000011732070445014223 5ustar alisonusersborderextract-1.3/src/main/0000755000175000001440000000000011732070445015147 5ustar alisonusersborderextract-1.3/src/main/java/0000755000175000001440000000000011732070445016070 5ustar alisonusersborderextract-1.3/src/main/java/com/0000755000175000001440000000000011732070445016646 5ustar alisonusersborderextract-1.3/src/main/java/com/generalbytes/0000755000175000001440000000000011732070445021332 5ustar alisonusersborderextract-1.3/src/main/java/com/generalbytes/osmosis/0000755000175000001440000000000011732070445023026 5ustar alisonusersborderextract-1.3/src/main/java/com/generalbytes/osmosis/borderextract/0000755000175000001440000000000011732070445025676 5ustar alisonusersborderextract-1.3/src/main/java/com/generalbytes/osmosis/borderextract/PolygonExporter.java0000644000175000001440000000351111717401124031714 0ustar alisonuserspackage 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.java0000644000175000001440000001202611717401124032024 0ustar alisonuserspackage 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.java0000644000175000001440000000332511666714162032502 0ustar alisonuserspackage 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.java0000644000175000001440000000455011666714162030004 0ustar alisonuserspackage 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.java0000644000175000001440000000444711717401124030174 0ustar alisonuserspackage 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.java0000644000175000001440000001444211702537303032137 0ustar alisonuserspackage 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); } } ././@LongLink0000000000000000000000000000014600000000000011566 Lustar rootrootborderextract-1.3/src/main/java/com/generalbytes/osmosis/borderextract/IncompleteBorderException.javaborderextract-1.3/src/main/java/com/generalbytes/osmosis/borderextract/IncompleteBorderException.jav0000644000175000001440000000503111666714162033522 0ustar alisonuserspackage 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.java0000644000175000001440000002623211724635333032270 0ustar alisonuserspackage 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.java0000644000175000001440000001417111724635333032522 0ustar alisonuserspackage 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.java0000644000175000001440000001140011717402024032317 0ustar alisonuserspackage 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.java0000644000175000001440000000725411725462041031164 0ustar alisonuserspackage 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.java0000644000175000001440000000422311724635333031471 0ustar alisonuserspackage 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.java0000644000175000001440000001311611724635333033472 0ustar alisonuserspackage 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.java0000644000175000001440000001014411724635333032753 0ustar alisonuserspackage 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/0000755000175000001440000000000011732070445015746 5ustar alisonusersborderextract-1.3/src/main/sql/setup_mysql.sql0000644000175000001440000000021511677047757021074 0ustar alisonusersCREATE 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/0000755000175000001440000000000011732070445017161 5ustar alisonusersborderextract-1.3/src/main/resources/osmosis-plugins.conf0000644000175000001440000000007211666461342023210 0ustar alisonuserscom.generalbytes.osmosis.borderextract.BorderExtractPluginborderextract-1.3/src/test/0000755000175000001440000000000011732070445015202 5ustar alisonusersborderextract-1.3/src/test/java/0000755000175000001440000000000011732070445016123 5ustar alisonusersborderextract-1.3/src/test/java/com/0000755000175000001440000000000011732070445016701 5ustar alisonusersborderextract-1.3/src/test/java/com/generalbytes/0000755000175000001440000000000011732070445021365 5ustar alisonusersborderextract-1.3/src/test/java/com/generalbytes/osmosis/0000755000175000001440000000000011732070445023061 5ustar alisonusersborderextract-1.3/src/test/java/com/generalbytes/osmosis/borderextract/0000755000175000001440000000000011732070445025731 5ustar alisonusersborderextract-1.3/src/test/java/com/generalbytes/osmosis/borderextract/EntityHelperTest.java0000644000175000001440000001151011725462041032045 0ustar alisonuserspackage 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.java0000644000175000001440000002126211724635333033161 0ustar alisonuserspackage 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.java0000644000175000001440000001511011724635333032022 0ustar alisonuserspackage 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.java0000644000175000001440000000633511717401124033407 0ustar alisonuserspackage 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(); } } ././@LongLink0000000000000000000000000000015100000000000011562 Lustar rootrootborderextract-1.3/src/test/java/com/generalbytes/osmosis/borderextract/BorderExtractTaskFactoryTest.javaborderextract-1.3/src/test/java/com/generalbytes/osmosis/borderextract/BorderExtractTaskFactoryTest.0000644000175000001440000000643611717401124033521 0ustar alisonuserspackage 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.java0000644000175000001440000001063711717401124033225 0ustar alisonuserspackage 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.txt0000644000175000001440000000206211702537303015255 0ustar alisonusersCopyright (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.xml0000644000175000001440000001055111725462041014752 0ustar alisonusers 4.0.0 com.generalbytes borderextract jar borderextract 1.3 Openstreetmap osmosis plugin to extract borders from osm. GB General Bytes GmbH http://www.general-bytes.com MET André Lison GB General Bytes GmbH http://www.general-bytes.com MIT License LICENSE.txt manually maven-compiler-plugin 1.6 1.6 UTF-8 true maven-resources-plugin UTF-8 com.google.guava guava 10.0.1 org.openstreetmap.osmosis osmosis-core 0.39 checkstyle checkstyle 4.4 test junit junit 4.8.1 test net.sourceforge.cobertura cobertura 1.9.4 test org.codehaus.mojo cobertura-maven-plugin org.apache.maven.plugins maven-checkstyle-plugin org.apache.maven.plugins maven-pmd-plugin true utf-8 1.6 org.apache.maven.plugins maven-javadoc-plugin http://java.sun.com/javase/6/docs/api/ en_US private maven2 maven2 http://repo2.maven.org/maven2