Search in sources :

Example 16 with EntityId

use of net.osmand.osm.edit.Entity.EntityId 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 17 with EntityId

use of net.osmand.osm.edit.Entity.EntityId 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 18 with EntityId

use of net.osmand.osm.edit.Entity.EntityId 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)

Example 19 with EntityId

use of net.osmand.osm.edit.Entity.EntityId in project OsmAnd-tools by osmandapp.

the class TagsTransformer method addPropogatedTags.

public void addPropogatedTags(Entity e) {
    EntityId eid = EntityId.valueOf(e);
    Map<String, String> proptags = propogatedTags.get(eid);
    if (proptags != null) {
        Iterator<Entry<String, String>> iterator = proptags.entrySet().iterator();
        while (iterator.hasNext()) {
            Entry<String, String> ts = iterator.next();
            if (e.getTag(ts.getKey()) == null) {
                String vl = ts.getValue();
                if (vl != null) {
                    vl = vl.replaceAll(SPLIT_VALUE, ", ");
                }
                e.putTag(ts.getKey(), vl);
            }
        }
    }
}
Also used : EntityId(net.osmand.osm.edit.Entity.EntityId) Entry(java.util.Map.Entry)

Example 20 with EntityId

use of net.osmand.osm.edit.Entity.EntityId in project OsmAnd-tools by osmandapp.

the class OsmStorageWriter method saveStorage.

public void saveStorage(OutputStream output, OsmBaseStorage storage, Collection<EntityId> interestedObjects, boolean includeLinks) throws XMLStreamException, IOException {
    Map<EntityId, Entity> entities = storage.getRegisteredEntities();
    Map<EntityId, EntityInfo> entityInfo = storage.getRegisteredEntityInfo();
    Set<Node> nodes = new LinkedHashSet<Node>();
    Set<Way> ways = new LinkedHashSet<Way>();
    Set<Relation> relations = new LinkedHashSet<Relation>();
    if (interestedObjects == null) {
        interestedObjects = entities.keySet();
    }
    Stack<EntityId> toResolve = new Stack<EntityId>();
    toResolve.addAll(interestedObjects);
    while (!toResolve.isEmpty()) {
        EntityId l = toResolve.pop();
        if (entities.get(l) instanceof Node) {
            nodes.add((Node) entities.get(l));
        } else if (entities.get(l) instanceof Way) {
            ways.add((Way) entities.get(l));
            if (includeLinks) {
                toResolve.addAll(((Way) entities.get(l)).getEntityIds());
            }
        } else if (entities.get(l) instanceof Relation) {
            relations.add((Relation) entities.get(l));
            if (includeLinks) {
                for (RelationMember rm : ((Relation) entities.get(l)).getMembers()) {
                    toResolve.add(rm.getEntityId());
                }
            }
        }
    }
    writeOSM(output, entityInfo, nodes, ways, relations);
}
Also used : LinkedHashSet(java.util.LinkedHashSet) Entity(net.osmand.osm.edit.Entity) Node(net.osmand.osm.edit.Node) Way(net.osmand.osm.edit.Way) Stack(java.util.Stack) EntityId(net.osmand.osm.edit.Entity.EntityId) Relation(net.osmand.osm.edit.Relation) RelationMember(net.osmand.osm.edit.Relation.RelationMember) EntityInfo(net.osmand.osm.edit.EntityInfo)

Aggregations

EntityId (net.osmand.osm.edit.Entity.EntityId)25 Entity (net.osmand.osm.edit.Entity)15 Node (net.osmand.osm.edit.Node)15 Way (net.osmand.osm.edit.Way)15 Relation (net.osmand.osm.edit.Relation)11 LinkedHashMap (java.util.LinkedHashMap)10 HashMap (java.util.HashMap)8 RelationMember (net.osmand.osm.edit.Relation.RelationMember)7 OsmBaseStorage (net.osmand.osm.io.OsmBaseStorage)7 FileInputStream (java.io.FileInputStream)5 FileOutputStream (java.io.FileOutputStream)5 OsmStorageWriter (net.osmand.osm.io.OsmStorageWriter)5 IOException (java.io.IOException)4 Map (java.util.Map)4 EntityInfo (net.osmand.osm.edit.EntityInfo)4 TLongObjectHashMap (gnu.trove.map.hash.TLongObjectHashMap)3 ArrayList (java.util.ArrayList)3 LinkedHashSet (java.util.LinkedHashSet)3 TLongArrayList (gnu.trove.list.array.TLongArrayList)2 ByteArrayInputStream (java.io.ByteArrayInputStream)2