Search in sources :

Example 1 with OsmBaseStorage

use of net.osmand.osm.io.OsmBaseStorage in project OsmAnd-tools by osmandapp.

the class FixAdminLevel0 method process.

private static void process(File read, File write) throws IOException, XMLStreamException, XmlPullParserException {
    OsmBaseStorage storage = new OsmBaseStorage();
    storage.parseOSM(new FileInputStream(read), new ConsoleProgressImplementation());
    Map<EntityId, Entity> entities = new HashMap<EntityId, Entity>(storage.getRegisteredEntities());
    long id = -1;
    for (EntityId e : entities.keySet()) {
        Entity es = storage.getRegisteredEntities().get(e);
        if (e.getId() < id) {
            id = e.getId() - 1;
        }
        if (e.getType() == EntityType.WAY) {
            processWay((Way) es);
        }
    }
    for (String country : countryNames.keySet()) {
        List<Way> list = countryNames.get(country);
        for (Way w : list) {
            LatLon latLon = OsmMapUtils.getMathWeightCenterForNodes(w.getNodes());
            // LatLon latLon = w.getLatLon();
            Node node = new Node(latLon.getLatitude(), latLon.getLongitude(), id--);
            node.putTag("name", country);
            node.putTag("place", "country");
            storage.getRegisteredEntities().put(EntityId.valueOf(node), node);
        }
    }
    OsmStorageWriter writer = new OsmStorageWriter();
    writer.saveStorage(new FileOutputStream(write), storage, null, true);
}
Also used : OsmStorageWriter(net.osmand.osm.io.OsmStorageWriter) Entity(net.osmand.osm.edit.Entity) HashMap(java.util.HashMap) LinkedHashMap(java.util.LinkedHashMap) Node(net.osmand.osm.edit.Node) ConsoleProgressImplementation(net.osmand.impl.ConsoleProgressImplementation) FileInputStream(java.io.FileInputStream) Way(net.osmand.osm.edit.Way) EntityId(net.osmand.osm.edit.Entity.EntityId) LatLon(net.osmand.data.LatLon) OsmBaseStorage(net.osmand.osm.io.OsmBaseStorage) FileOutputStream(java.io.FileOutputStream)

Example 2 with OsmBaseStorage

use of net.osmand.osm.io.OsmBaseStorage in project OsmAnd-tools by osmandapp.

the class OceanTilesCreator method createTilesFile.

public static void createTilesFile(String coastlinesInput, String result) throws IOException, XmlPullParserException {
    if (result == null) {
        result = "oceantiles_12.dat";
    }
    File readFile = new File(coastlinesInput);
    InputStream stream = new BufferedInputStream(new FileInputStream(readFile), 8192 * 4);
    InputStream streamFile = stream;
    long st = System.currentTimeMillis();
    if (readFile.getName().endsWith(".bz2")) {
        // $NON-NLS-1$
        if (stream.read() != 'B' || stream.read() != 'Z') {
        // throw new RuntimeException("The source stream must start with the characters BZ if it is to be read as a BZip2 stream."); //$NON-NLS-1$
        } else {
            stream = new CBZip2InputStream(stream);
        }
    }
    OsmBaseStorage bs = new OsmBaseStorage();
    bs.parseOSM(stream, IProgress.EMPTY_PROGRESS);
    int c = 0;
    int ns = 0;
    TLongObjectHashMap<OceanTileInfo> map = new TLongObjectHashMap<OceanTileInfo>();
    for (Entity e : bs.getRegisteredEntities().values()) {
        if (e instanceof Way) {
            Way w = (Way) e;
            List<Node> nodes = w.getNodes();
            for (int i = 1; i < nodes.size(); i++) {
                double tx = MapUtils.getTileNumberX(TILE_ZOOMLEVEL, nodes.get(i).getLongitude());
                double ty = MapUtils.getTileNumberY(TILE_ZOOMLEVEL, nodes.get(i).getLatitude());
                double px = MapUtils.getTileNumberX(TILE_ZOOMLEVEL, nodes.get(i - 1).getLongitude());
                double py = MapUtils.getTileNumberY(TILE_ZOOMLEVEL, nodes.get(i - 1).getLatitude());
                for (int x = (int) Math.min(tx, px); x <= Math.max(tx, px); x++) {
                    for (int y = (int) Math.min(ty, py); y <= Math.max(ty, py); y++) {
                        // check if intersects (x-1,y+0.5) & (x,y+0.5)
                        long key = ((long) x << TILE_ZOOMLEVEL) + (long) y;
                        if (intersect2Segments(tx, ty, px, py, x, y + 0.5d, x + 1, y + 0.5d)) {
                            getOrCreate(map, key).linesIntersectMedian++;
                            getOrCreate(map, key).type = OceanTileInfo.MIXED;
                        } else if (intersect2Segments(tx, ty, px, py, x, y, x + 1, y)) {
                            getOrCreate(map, key).type = OceanTileInfo.MIXED;
                        } else if (intersect2Segments(tx, ty, px, py, x, y + 1, x + 1, y + 1)) {
                            getOrCreate(map, key).type = OceanTileInfo.MIXED;
                        } else if (intersect2Segments(tx, ty, px, py, x, y, x, y + 1)) {
                            getOrCreate(map, key).type = OceanTileInfo.MIXED;
                        } else if (intersect2Segments(tx, ty, px, py, x + 1, y, x + 1, y + 1)) {
                            getOrCreate(map, key).type = OceanTileInfo.MIXED;
                        }
                    }
                }
            }
            c++;
            ns += w.getNodeIds().size();
        }
    }
    writeResult(map, result);
    System.out.println(c + " " + ns + " coastlines " + map.size());
}
Also used : Entity(net.osmand.osm.edit.Entity) BufferedInputStream(java.io.BufferedInputStream) FileInputStream(java.io.FileInputStream) CBZip2InputStream(org.apache.tools.bzip2.CBZip2InputStream) InputStream(java.io.InputStream) CBZip2InputStream(org.apache.tools.bzip2.CBZip2InputStream) Node(net.osmand.osm.edit.Node) TLongObjectHashMap(gnu.trove.map.hash.TLongObjectHashMap) FileInputStream(java.io.FileInputStream) Way(net.osmand.osm.edit.Way) BufferedInputStream(java.io.BufferedInputStream) OsmBaseStorage(net.osmand.osm.io.OsmBaseStorage) RandomAccessFile(java.io.RandomAccessFile) File(java.io.File)

Example 3 with OsmBaseStorage

use of net.osmand.osm.io.OsmBaseStorage in project OsmAnd-tools by osmandapp.

the class OceanTilesCreator method createJOSMFile.

public static void createJOSMFile(String[] args) throws XMLStreamException, IOException {
    String fileLocation = args.length == 0 ? "oceanTiles.osm" : args[0];
    int z = TILE_ZOOMLEVEL;
    BasemapProcessor bmp = new BasemapProcessor();
    bmp.constructBitSetInfo(args.length > 1 ? args[1] : null);
    OsmBaseStorage st = new OsmBaseStorage();
    Set<Entity.EntityId> s = new LinkedHashSet();
    TLongHashSet nodeIds = new TLongHashSet();
    int minzoom = 4;
    BasemapProcessor.SimplisticQuadTree quadTree = bmp.constructTilesQuadTree(z);
    for (int zm = minzoom; zm <= z; zm++) {
        int pz = 1 << zm;
        for (int x = 0; x < pz; x++) {
            for (int y = 0; y < pz; y++) {
                // if((quadTree.getOrCreateSubTree(i, j, z).seaCharacteristic < 0.9 && !bmp.isWaterTile(i, j, z))||
                // bmp.isLandTile(i, j, z) ) {
                boolean parentWater = bmp.isWaterTile(x >> 1, y >> 1, zm - 1);
                boolean parentLand = bmp.isLandTile(x >> 1, y >> 1, zm - 1);
                if (zm > minzoom && (parentLand || parentWater)) {
                    continue;
                }
                boolean landTile = bmp.isLandTile(x, y, zm);
                boolean waterTile = bmp.isWaterTile(x, y, zm);
                if (waterTile || landTile) {
                    Way w = new Way(-getNodeId(x, y, zm));
                    addNode(w, nodeIds, x, y, zm);
                    addNode(w, nodeIds, x, y + 1, zm);
                    addNode(w, nodeIds, x + 1, y + 1, zm);
                    addNode(w, nodeIds, x + 1, y, zm);
                    addNode(w, nodeIds, x, y, zm);
                    if (waterTile) {
                        w.putTag("natural", "water");
                    } else if (landTile) {
                        w.putTag("landuse", "grass");
                    }
                    w.putTag("name", x + " " + y + " " + zm + " " + (waterTile ? 1 : 0));
                    s.addAll(w.getEntityIds());
                    s.add(Entity.EntityId.valueOf(w));
                    st.registerEntity(w, null);
                }
            }
        }
    }
    for (long l : nodeIds.toArray()) {
        st.registerEntity(getNode(l), null);
    }
    new OsmStorageWriter().saveStorage(new FileOutputStream(fileLocation), st, s, true);
}
Also used : LinkedHashSet(java.util.LinkedHashSet) OsmStorageWriter(net.osmand.osm.io.OsmStorageWriter) Way(net.osmand.osm.edit.Way) OsmBaseStorage(net.osmand.osm.io.OsmBaseStorage) TLongHashSet(gnu.trove.set.hash.TLongHashSet) FileOutputStream(java.io.FileOutputStream)

Example 4 with OsmBaseStorage

use of net.osmand.osm.io.OsmBaseStorage in project OsmAnd-tools by osmandapp.

the class GenerateRegionTags method process.

private static void process(File inputFile, File targetFile, OsmandRegions or) throws IOException, XmlPullParserException, XMLStreamException {
    InputStream fis = new FileInputStream(inputFile);
    if (inputFile.getName().endsWith(".gz")) {
        fis = new GZIPInputStream(fis);
    } else if (inputFile.getName().endsWith(".bz2")) {
        if (fis.read() != 'B' || fis.read() != 'Z') {
            throw new RuntimeException(// $NON-NLS-1$
            "The source stream must start with the characters BZ if it is to be read as a BZip2 stream.");
        }
        fis = new CBZip2InputStream(fis);
    }
    OsmBaseStorage bs = new OsmBaseStorage();
    bs.parseOSM(fis, new ConsoleProgressImplementation());
    LOG.info("File was read");
    iterateOverEntities(bs.getRegisteredEntities(), or);
    OsmStorageWriter w = new OsmStorageWriter();
    OutputStream output = new FileOutputStream(targetFile);
    if (targetFile.getName().endsWith(".gz")) {
        output = new GZIPOutputStream(output);
    } else if (targetFile.getName().endsWith(".bz2")) {
        output.write("BZ".getBytes());
        output = new CBZip2OutputStream(output);
    }
    LOG.info("Entities processed. About to save the file.");
    w.saveStorage(output, bs, null, true);
    output.close();
    fis.close();
}
Also used : GZIPInputStream(java.util.zip.GZIPInputStream) OsmStorageWriter(net.osmand.osm.io.OsmStorageWriter) CBZip2OutputStream(org.apache.tools.bzip2.CBZip2OutputStream) GZIPOutputStream(java.util.zip.GZIPOutputStream) GZIPInputStream(java.util.zip.GZIPInputStream) CBZip2InputStream(org.apache.tools.bzip2.CBZip2InputStream) FileInputStream(java.io.FileInputStream) InputStream(java.io.InputStream) OsmBaseStorage(net.osmand.osm.io.OsmBaseStorage) CBZip2InputStream(org.apache.tools.bzip2.CBZip2InputStream) OutputStream(java.io.OutputStream) CBZip2OutputStream(org.apache.tools.bzip2.CBZip2OutputStream) FileOutputStream(java.io.FileOutputStream) GZIPOutputStream(java.util.zip.GZIPOutputStream) FileOutputStream(java.io.FileOutputStream) ConsoleProgressImplementation(net.osmand.impl.ConsoleProgressImplementation) FileInputStream(java.io.FileInputStream)

Example 5 with OsmBaseStorage

use of net.osmand.osm.io.OsmBaseStorage in project OsmAnd-tools by osmandapp.

the class MultipolygonFileTest method testDifferentOrientationMultipolygon.

@Test
public void testDifferentOrientationMultipolygon() throws IOException, XmlPullParserException {
    String file = "multipolygon.osm";
    OsmBaseStorage st = parse(file);
    Multipolygon polygon = buildPolygon(st, 1184817L);
    assertTrue(polygon.areRingsComplete());
    assertEquals(1, polygon.getOuterRings().size());
    Ring rng = polygon.getOuterRings().get(0);
    assertTrue(rng.containsPoint(53.17573, 8.26));
    assertTrue(rng.containsPoint(53.18901289819956, 8.296700487828224));
    assertFalse(rng.containsPoint(53.1863199155393, 8.309607569738336));
    assertFalse(rng.containsPoint(53.13992097340422, 8.280586804995954));
}
Also used : OsmBaseStorage(net.osmand.osm.io.OsmBaseStorage) Test(org.junit.Test)

Aggregations

OsmBaseStorage (net.osmand.osm.io.OsmBaseStorage)14 FileInputStream (java.io.FileInputStream)8 Entity (net.osmand.osm.edit.Entity)7 EntityId (net.osmand.osm.edit.Entity.EntityId)7 FileOutputStream (java.io.FileOutputStream)6 Node (net.osmand.osm.edit.Node)6 OsmStorageWriter (net.osmand.osm.io.OsmStorageWriter)6 InputStream (java.io.InputStream)5 HashMap (java.util.HashMap)5 LinkedHashMap (java.util.LinkedHashMap)5 ConsoleProgressImplementation (net.osmand.impl.ConsoleProgressImplementation)5 Way (net.osmand.osm.edit.Way)5 CBZip2InputStream (org.apache.tools.bzip2.CBZip2InputStream)4 BufferedInputStream (java.io.BufferedInputStream)3 IOException (java.io.IOException)3 GZIPInputStream (java.util.zip.GZIPInputStream)3 LatLon (net.osmand.data.LatLon)3 XmlPullParserException (org.xmlpull.v1.XmlPullParserException)3 TLongObjectHashMap (gnu.trove.map.hash.TLongObjectHashMap)2 ByteArrayInputStream (java.io.ByteArrayInputStream)2