Search in sources :

Example 6 with OsmBaseStorage

use of net.osmand.osm.io.OsmBaseStorage in project Osmand by osmandapp.

the class OpenstreetmapRemoteUtil method loadNode.

public EntityInfo loadNode(Node n) {
    // >> 1;
    long nodeId = n.getId();
    try {
        String res = sendRequest(getSiteApi() + "api/0.6/node/" + nodeId, "GET", null, ctx.getString(R.string.loading_poi_obj) + nodeId, // $NON-NLS-1$ //$NON-NLS-2$
        false);
        if (res != null) {
            OsmBaseStorage st = new OsmBaseStorage();
            st.setConvertTagsToLC(false);
            // $NON-NLS-1$
            st.parseOSM(new ByteArrayInputStream(res.getBytes("UTF-8")), null, null, true);
            EntityId id = new Entity.EntityId(EntityType.NODE, nodeId);
            Node entity = (Node) st.getRegisteredEntities().get(id);
            // merge non existing tags
            Map<String, String> updatedTags = new HashMap<>();
            for (String tagKey : entity.getTagKeySet()) {
                if (tagKey != null && !deletedTag(n, tagKey)) {
                    addIfNotNull(tagKey, entity.getTag(tagKey), updatedTags);
                }
            }
            if (n.getChangedTags() != null) {
                for (String tagKey : n.getChangedTags()) {
                    if (tagKey != null) {
                        addIfNotNull(tagKey, n.getTag(tagKey), updatedTags);
                    }
                }
            }
            n.replaceTags(updatedTags);
            if (MapUtils.getDistance(n.getLatLon(), entity.getLatLon()) < 10) {
                // avoid shifting due to round error
                n.setLatitude(entity.getLatitude());
                n.setLongitude(entity.getLongitude());
            }
            entityInfo = st.getRegisteredEntityInfo().get(id);
            entityInfoId = id;
            return entityInfo;
        }
    } catch (IOException | XmlPullParserException e) {
        // $NON-NLS-1$
        log.error("Loading node failed " + nodeId, e);
        Toast.makeText(ctx, ctx.getResources().getString(R.string.shared_string_io_error), Toast.LENGTH_LONG).show();
    }
    return null;
}
Also used : EntityId(net.osmand.osm.edit.Entity.EntityId) ByteArrayInputStream(java.io.ByteArrayInputStream) HashMap(java.util.HashMap) LinkedHashMap(java.util.LinkedHashMap) OsmBaseStorage(net.osmand.osm.io.OsmBaseStorage) Node(net.osmand.osm.edit.Node) XmlPullParserException(org.xmlpull.v1.XmlPullParserException) IOException(java.io.IOException)

Example 7 with OsmBaseStorage

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

the class IndexCreator method extractOsmToNodesDB.

private OsmDbCreator extractOsmToNodesDB(OsmDbAccessor accessor, File readFile, IProgress progress, IOsmStorageFilter addFilter, int additionId, int shiftId, boolean ovewriteIds, boolean generateNewIds, boolean createTables, OsmDbCreator previous) throws IOException, SQLException, XmlPullParserException {
    boolean pbfFile = false;
    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);
        }
    } else if (readFile.getName().endsWith(".gz")) {
        // $NON-NLS-1$
        stream = new GZIPInputStream(stream);
    } else if (readFile.getName().endsWith(".pbf")) {
        // $NON-NLS-1$
        pbfFile = true;
    }
    OsmBaseStorage storage = pbfFile ? new OsmBaseStoragePbf() : new OsmBaseStorage();
    storage.setSupressWarnings(DataExtractionSettings.getSettings().isSupressWarningsForDuplicatedId());
    if (addFilter != null) {
        storage.getFilters().add(addFilter);
    }
    storage.getFilters().add(new IOsmStorageFilter() {

        @Override
        public boolean acceptEntityToLoad(OsmBaseStorage storage, EntityId entityId, Entity entity) {
            if (indexAddressCreator != null) {
                indexAddressCreator.registerCityIfNeeded(entity);
            }
            // accept to allow db creator parse it
            return true;
        }
    });
    // 1. Loading osm file
    OsmDbCreator dbCreator = new OsmDbCreator(additionId, shiftId, ovewriteIds, generateNewIds);
    if (previous != null) {
        dbCreator.setNodeIds(previous.getNodeIds());
        dbCreator.setWayIds(previous.getWayIds());
        dbCreator.setRelationIds(previous.getRelationIds());
    }
    dbCreator.setBackwardCompatibleIds(backwardCompatibleIds);
    try {
        // $NON-NLS-1$
        setGeneralProgress(progress, "[15 / 100]");
        // $NON-NLS-1$
        progress.startTask(Messages.getString("IndexCreator.LOADING_FILE") + readFile.getAbsolutePath(), -1);
        // 1 init database to store temporary data
        dbCreator.initDatabase(osmDBdialect, accessor.getDbConn(), createTables);
        storage.getFilters().add(dbCreator);
        if (pbfFile) {
            ((OsmBaseStoragePbf) storage).parseOSMPbf(stream, progress, false);
        } else {
            storage.parseOSM(stream, progress, streamFile, false);
        }
        dbCreator.finishLoading();
        osmDBdialect.commitDatabase(accessor.getDbConn());
        if (log.isInfoEnabled()) {
            // $NON-NLS-1$
            log.info("File parsed : " + (System.currentTimeMillis() - st));
        }
        progress.finishTask();
        return dbCreator;
    } finally {
        if (log.isInfoEnabled()) {
            // $NON-NLS-1$
            log.info("File indexed : " + (System.currentTimeMillis() - st));
        }
    }
}
Also used : Entity(net.osmand.osm.edit.Entity) GZIPInputStream(java.util.zip.GZIPInputStream) BufferedInputStream(java.io.BufferedInputStream) CBZip2InputStream(org.apache.tools.bzip2.CBZip2InputStream) FileInputStream(java.io.FileInputStream) InputStream(java.io.InputStream) OsmBaseStoragePbf(net.osmand.osm.io.OsmBaseStoragePbf) IOsmStorageFilter(net.osmand.osm.io.IOsmStorageFilter) CBZip2InputStream(org.apache.tools.bzip2.CBZip2InputStream) FileInputStream(java.io.FileInputStream) GZIPInputStream(java.util.zip.GZIPInputStream) EntityId(net.osmand.osm.edit.Entity.EntityId) BufferedInputStream(java.io.BufferedInputStream) OsmBaseStorage(net.osmand.osm.io.OsmBaseStorage)

Example 8 with OsmBaseStorage

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

the class FixBasemapRoads method process.

private void process(File read, File write, List<File> relationFiles) throws IOException, XMLStreamException, XmlPullParserException, SQLException {
    MapRenderingTypesEncoder renderingTypes = new MapRenderingTypesEncoder("basemap");
    OsmandRegions or = prepareRegions();
    TagsTransformer transformer = new TagsTransformer();
    for (File relFile : relationFiles) {
        LOG.info("Parse relations file " + relFile.getName());
        OsmBaseStorage storage = parseOsmFile(relFile);
        int total = 0;
        for (EntityId e : storage.getRegisteredEntities().keySet()) {
            if (e.getType() == EntityType.RELATION) {
                total++;
                if (total % 1000 == 0) {
                    LOG.info("Processed " + total + " relations");
                }
                Relation es = (Relation) storage.getRegisteredEntities().get(e);
                transformer.handleRelationPropogatedTags(es, renderingTypes, null, EntityConvertApplyType.MAP);
            }
        }
    }
    LOG.info("Parse main file " + read.getName());
    OsmBaseStorage storage = parseOsmFile(read);
    Map<EntityId, Entity> entities = new HashMap<EntityId, Entity>(storage.getRegisteredEntities());
    int total = 0;
    for (EntityId e : entities.keySet()) {
        if (e.getType() == EntityType.WAY) {
            Way es = (Way) storage.getRegisteredEntities().get(e);
            total++;
            if (total % 1000 == 0) {
                LOG.info("Processed " + total + " ways");
            }
            addRegionTag(or, es);
            transformer.addPropogatedTags(es);
            Map<String, String> ntags = renderingTypes.transformTags(es.getModifiableTags(), EntityType.WAY, EntityConvertApplyType.MAP);
            if (es.getModifiableTags() != ntags) {
                es.getModifiableTags().putAll(ntags);
            }
            processWay(es);
        }
    }
    List<EntityId> toWrite = new ArrayList<EntityId>();
    processRegion(toWrite);
    OsmStorageWriter writer = new OsmStorageWriter();
    LOG.info("Writing file... ");
    writer.saveStorage(new FileOutputStream(write), storage, toWrite, true);
    LOG.info("DONE");
}
Also used : OsmStorageWriter(net.osmand.osm.io.OsmStorageWriter) Entity(net.osmand.osm.edit.Entity) HashMap(java.util.HashMap) LinkedHashMap(java.util.LinkedHashMap) TLongObjectHashMap(gnu.trove.map.hash.TLongObjectHashMap) TagsTransformer(net.osmand.data.preparation.TagsTransformer) ArrayList(java.util.ArrayList) TLongArrayList(gnu.trove.list.array.TLongArrayList) Way(net.osmand.osm.edit.Way) EntityId(net.osmand.osm.edit.Entity.EntityId) Relation(net.osmand.osm.edit.Relation) MapRenderingTypesEncoder(net.osmand.osm.MapRenderingTypesEncoder) OsmandRegions(net.osmand.map.OsmandRegions) OsmBaseStorage(net.osmand.osm.io.OsmBaseStorage) FileOutputStream(java.io.FileOutputStream) File(java.io.File)

Example 9 with OsmBaseStorage

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

the class FixBasemapRoads method parseOsmFile.

private OsmBaseStorage parseOsmFile(File read) throws FileNotFoundException, IOException, XmlPullParserException {
    OsmBaseStorage storage = new OsmBaseStorage();
    InputStream stream = new BufferedInputStream(new FileInputStream(read), 8192 * 4);
    InputStream streamFile = stream;
    if (read.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);
        }
    } else if (read.getName().endsWith(".gz")) {
        // $NON-NLS-1$
        stream = new GZIPInputStream(stream);
    }
    storage.parseOSM(stream, new ConsoleProgressImplementation(), streamFile, true);
    return storage;
}
Also used : GZIPInputStream(java.util.zip.GZIPInputStream) BufferedInputStream(java.io.BufferedInputStream) OsmBaseStorage(net.osmand.osm.io.OsmBaseStorage) GZIPInputStream(java.util.zip.GZIPInputStream) BufferedInputStream(java.io.BufferedInputStream) CBZip2InputStream(org.apache.tools.bzip2.CBZip2InputStream) FileInputStream(java.io.FileInputStream) InputStream(java.io.InputStream) CBZip2InputStream(org.apache.tools.bzip2.CBZip2InputStream) ConsoleProgressImplementation(net.osmand.impl.ConsoleProgressImplementation) FileInputStream(java.io.FileInputStream)

Example 10 with OsmBaseStorage

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

the class FixLinkedCoastline 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());
    List<EntityId> toWrite = new ArrayList<EntityId>();
    for (EntityId e : entities.keySet()) {
        if (e.getType() == EntityType.WAY) {
            Entity oldWay = storage.getRegisteredEntities().remove(e);
            List<Way> result = processWay((Way) oldWay);
            alignAndAddtoStorage(storage, toWrite, result);
        }
    }
    System.out.println("ERROR Ways : ");
    int errors = 0;
    for (List<Way> w : endWays.values()) {
        Way way = w.get(0);
        Way lway = w.get(w.size() - 1);
        LatLon first = way.getNodes().get(0).getLatLon();
        LatLon last = lway.getNodes().get(lway.getNodes().size() - 1).getLatLon();
        double dist = MapUtils.getDistance(first, last);
        if (dist < 500000) {
            alignAndAddtoStorage(storage, toWrite, w);
        } else {
            errors++;
            String val = "First " + first + "Last " + last + " id " + way.getId() + " dist " + MapUtils.getDistance(first, last) + " m";
            System.out.println("Ways in chain - " + w.size() + " - " + val);
        }
    }
    System.out.println("Fixed errors : " + ERRORS + ", errors not fixed : " + errors);
    OsmStorageWriter writer = new OsmStorageWriter();
    /*Set<Long> ls = new HashSet<Long>();
        for(EntityId e : toWrite) {
            if(!ls.add(e.getId())) {
                System.out.println("!!" + e.getId());
            }
        }*/
    writer.saveStorage(new FileOutputStream(write), storage, toWrite, true);
}
Also used : OsmStorageWriter(net.osmand.osm.io.OsmStorageWriter) Entity(net.osmand.osm.edit.Entity) 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)

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