Search in sources :

Example 16 with Relation

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

the class AugmentedDiffsInspector method writeFile.

private File writeFile(File targetDir, String prefix, Map<EntityId, Entity> octx, Set<EntityId> oset, Map<EntityId, Entity> nctx, Set<EntityId> nset, long lastModified) throws XMLStreamException, IOException, FileNotFoundException {
    List<Node> nodes = new ArrayList<Node>();
    List<Way> ways = new ArrayList<Way>();
    List<Relation> relations = new ArrayList<Relation>();
    groupObjects(octx, oset, nodes, ways, relations);
    groupObjects(nctx, nset, nodes, ways, relations);
    File f = new File(targetDir, prefix + ".osm.gz");
    FileOutputStream fous = new FileOutputStream(f);
    GZIPOutputStream gz = new GZIPOutputStream(fous);
    new OsmStorageWriter().writeOSM(gz, new HashMap<Entity.EntityId, EntityInfo>(), nodes, ways, relations, true);
    gz.close();
    fous.close();
    f.setLastModified(lastModified);
    return f;
}
Also used : OsmStorageWriter(net.osmand.osm.io.OsmStorageWriter) Node(net.osmand.osm.edit.Node) ArrayList(java.util.ArrayList) Way(net.osmand.osm.edit.Way) EntityId(net.osmand.osm.edit.Entity.EntityId) Relation(net.osmand.osm.edit.Relation) GZIPOutputStream(java.util.zip.GZIPOutputStream) EntityInfo(net.osmand.osm.edit.EntityInfo) FileOutputStream(java.io.FileOutputStream) File(java.io.File)

Example 17 with Relation

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

the class MultipolygonFileTest method buildPolygon.

private Multipolygon buildPolygon(OsmBaseStorage st, long id) {
    Relation r = (Relation) st.getRegisteredEntities().get(new EntityId(EntityType.RELATION, id));
    Iterator<RelationMember> it = r.getMembers().iterator();
    MultipolygonBuilder bld = new MultipolygonBuilder();
    while (it.hasNext()) {
        RelationMember e = it.next();
        if (e.getRole().equals("outer")) {
            bld.addOuterWay((Way) e.getEntity());
        } else if (e.getRole().equals("inner")) {
            bld.addInnerWay((Way) e.getEntity());
        }
    }
    Multipolygon polygon = bld.build();
    return polygon;
}
Also used : EntityId(net.osmand.osm.edit.Entity.EntityId) Relation(net.osmand.osm.edit.Relation) RelationMember(net.osmand.osm.edit.Relation.RelationMember) Way(net.osmand.osm.edit.Way)

Example 18 with Relation

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

the class IndexCreator method generateBasemapIndex.

public void generateBasemapIndex(IProgress progress, IOsmStorageFilter addFilter, MapZooms mapZooms, MapRenderingTypesEncoder renderingTypes, Log logMapDataWarn, String regionName, File... readFiles) throws IOException, SQLException, InterruptedException, XmlPullParserException {
    if (logMapDataWarn == null) {
        logMapDataWarn = log;
    }
    if (renderingTypes == null) {
        renderingTypes = new MapRenderingTypesEncoder("basemap");
    }
    if (mapZooms == null) {
        mapZooms = MapZooms.getDefault();
    }
    // clear previous results and setting variables
    try {
        final BasemapProcessor processor = new BasemapProcessor(logMapDataWarn, mapZooms, renderingTypes, zoomWaySmoothness);
        final IndexPoiCreator poiCreator = indexPOI ? new IndexPoiCreator(renderingTypes, false) : null;
        if (indexPOI) {
            poiCreator.createDatabaseStructure(getPoiFile());
        }
        OsmDbAccessor accessor = initDbAccessor(readFiles, progress, addFilter, true, false, true);
        // 2. Create index connections and index structure
        setGeneralProgress(progress, "[50 / 100]");
        progress.startTask(Messages.getString("IndexCreator.PROCESS_OSM_NODES"), accessor.getAllNodes());
        accessor.iterateOverEntities(progress, EntityType.NODE, new OsmDbVisitor() {

            @Override
            public void iterateEntity(Entity e, OsmDbAccessorContext ctx) throws SQLException {
                processor.processEntity(e);
                if (indexPOI) {
                    poiCreator.iterateEntity(e, ctx, true);
                }
            }
        });
        setGeneralProgress(progress, "[70 / 100]");
        progress.startTask(Messages.getString("IndexCreator.PROCESS_OSM_WAYS"), accessor.getAllWays());
        accessor.iterateOverEntities(progress, EntityType.WAY, new OsmDbVisitor() {

            @Override
            public void iterateEntity(Entity e, OsmDbAccessorContext ctx) throws SQLException {
                processor.processEntity(e);
                if (indexPOI) {
                    poiCreator.iterateEntity(e, ctx, true);
                }
            }
        });
        setGeneralProgress(progress, "[90 / 100]");
        progress.startTask(Messages.getString("IndexCreator.PROCESS_OSM_REL"), accessor.getAllRelations());
        accessor.iterateOverEntities(progress, EntityType.RELATION, new OsmDbVisitor() {

            @Override
            public void iterateEntity(Entity e, OsmDbAccessorContext ctx) throws SQLException {
                ctx.loadEntityRelation((Relation) e);
                processor.processEntity(e);
            }
        });
        accessor.closeReadingConnection();
        mapFile = new File(workingDir, getMapFileName());
        // to save space
        mapFile.getParentFile().mkdirs();
        if (mapFile.exists()) {
            mapFile.delete();
        }
        mapRAFile = new RandomAccessFile(mapFile, "rw");
        BinaryMapIndexWriter writer = new BinaryMapIndexWriter(mapRAFile, lastModifiedDate == null ? System.currentTimeMillis() : lastModifiedDate.longValue());
        setGeneralProgress(progress, "[95 of 100]");
        progress.startTask("Writing map index to binary file...", -1);
        processor.writeBasemapFile(writer, regionName);
        if (indexPOI) {
            poiCreator.writeBinaryPoiIndex(writer, regionName, progress);
        }
        progress.finishTask();
        writer.close();
        mapRAFile.close();
        // $NON-NLS-1$
        log.info("Finish writing binary file");
    } catch (RuntimeException e) {
        // $NON-NLS-1$
        log.error("Log exception", e);
        throw e;
    } catch (SQLException e) {
        // $NON-NLS-1$
        log.error("Log exception", e);
        throw e;
    } catch (IOException e) {
        // $NON-NLS-1$
        log.error("Log exception", e);
        throw e;
    } catch (XmlPullParserException e) {
        // $NON-NLS-1$
        log.error("Log exception", e);
        throw e;
    }
}
Also used : Entity(net.osmand.osm.edit.Entity) SQLException(java.sql.SQLException) IOException(java.io.IOException) Relation(net.osmand.osm.edit.Relation) MapRenderingTypesEncoder(net.osmand.osm.MapRenderingTypesEncoder) RandomAccessFile(java.io.RandomAccessFile) OsmDbVisitor(net.osmand.data.preparation.OsmDbAccessor.OsmDbVisitor) XmlPullParserException(org.xmlpull.v1.XmlPullParserException) RandomAccessFile(java.io.RandomAccessFile) File(java.io.File)

Example 19 with Relation

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

the class IndexPoiCreator method iterateEntity.

public void iterateEntity(Entity e, OsmDbAccessorContext ctx, boolean basemap) throws SQLException {
    tempAmenityList.clear();
    tagsTransform.addPropogatedTags(e);
    Map<String, String> tags = e.getTags();
    Map<String, String> etags = renderingTypes.transformTags(tags, EntityType.valueOf(e), EntityConvertApplyType.POI);
    boolean privateReg = "private".equals(e.getTag("access"));
    tempAmenityList = EntityParser.parseAmenities(poiTypes, e, etags, tempAmenityList);
    if (!tempAmenityList.isEmpty() && poiPreparedStatement != null) {
        if (e instanceof Relation) {
            ctx.loadEntityRelation((Relation) e);
        }
        boolean first = true;
        long id = e.getId();
        if (basemap) {
            id = GENERATE_OBJ_ID--;
        } else if (e instanceof Relation) {
            // id = GENERATE_OBJ_ID--;
            id = assignIdForMultipolygon((Relation) e);
        } else if (id > 0) {
            // keep backward compatibility for ids (osm editing)
            id = e.getId() >> (OsmDbCreator.SHIFT_ID - 1);
            if (id % 2 != (e.getId() % 2)) {
                id ^= 1;
            }
        }
        for (Amenity a : tempAmenityList) {
            if (a.getType().getKeyName().equals("entertainment") && privateReg) {
                // don't index private swimming pools
                continue;
            }
            if (basemap) {
                PoiType st = a.getType().getPoiTypeByKeyName(a.getSubType());
                if (st == null || !a.getType().containsBasemapPoi(st)) {
                    continue;
                }
            }
            // do not add that check because it is too much printing for batch creation
            // by statistic < 1% creates maps manually
            // checkEntity(e);
            EntityParser.parseMapObject(a, e, etags);
            a.setId(id);
            if (a.getLocation() != null) {
                // convertEnglishName(a);
                if (overwriteIds && first) {
                    if (!ids.add(a.getId())) {
                        poiPreparedStatement.executeBatch();
                        poiDeleteStatement.setString(1, a.getId() + "");
                        poiDeleteStatement.execute();
                        first = false;
                    }
                }
                insertAmenityIntoPoi(a);
            }
        }
    }
}
Also used : Amenity(net.osmand.data.Amenity) Relation(net.osmand.osm.edit.Relation) PoiType(net.osmand.osm.PoiType)

Example 20 with Relation

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

the class IndexRouteCreator method indexHighwayRestrictions.

private void indexHighwayRestrictions(Entity e, OsmDbAccessorContext ctx) throws SQLException {
    if (e instanceof Relation && "restriction".equals(e.getTag(OSMTagKey.TYPE))) {
        // $NON-NLS-1$
        // $NON-NLS-1$
        String val = e.getTag("restriction");
        if (val != null) {
            if ("no_u_turn".equalsIgnoreCase(val) && Algorithms.objectEquals(e.getTag("from"), e.getTag("to"))) {
                // don't index such roads - can't go through issue https://www.openstreetmap.org/way/338099991#map=17/46.86699/-0.20473
                return;
            }
            byte type = -1;
            if ("no_right_turn".equalsIgnoreCase(val)) {
                // $NON-NLS-1$
                type = MapRenderingTypes.RESTRICTION_NO_RIGHT_TURN;
            } else if ("no_left_turn".equalsIgnoreCase(val)) {
                // $NON-NLS-1$
                type = MapRenderingTypes.RESTRICTION_NO_LEFT_TURN;
            } else if ("no_u_turn".equalsIgnoreCase(val)) {
                // $NON-NLS-1$
                type = MapRenderingTypes.RESTRICTION_NO_U_TURN;
            } else if ("no_straight_on".equalsIgnoreCase(val)) {
                // $NON-NLS-1$
                type = MapRenderingTypes.RESTRICTION_NO_STRAIGHT_ON;
            } else if ("only_right_turn".equalsIgnoreCase(val)) {
                // $NON-NLS-1$
                type = MapRenderingTypes.RESTRICTION_ONLY_RIGHT_TURN;
            } else if ("only_left_turn".equalsIgnoreCase(val)) {
                // $NON-NLS-1$
                type = MapRenderingTypes.RESTRICTION_ONLY_LEFT_TURN;
            } else if ("only_straight_on".equalsIgnoreCase(val)) {
                // $NON-NLS-1$
                type = MapRenderingTypes.RESTRICTION_ONLY_STRAIGHT_ON;
            }
            if (type != -1) {
                ctx.loadEntityRelation((Relation) e);
                // $NON-NLS-1$
                Collection<RelationMember> fromL = ((Relation) e).getMembers("from");
                // $NON-NLS-1$
                Collection<RelationMember> toL = ((Relation) e).getMembers("to");
                if (!fromL.isEmpty() && !toL.isEmpty()) {
                    RelationMember from = fromL.iterator().next();
                    RelationMember to = toL.iterator().next();
                    if (from.getEntityId().getType() == EntityType.WAY) {
                        if (!highwayRestrictions.containsKey(from.getEntityId().getId())) {
                            highwayRestrictions.put(from.getEntityId().getId(), new TLongArrayList());
                        }
                        highwayRestrictions.get(from.getEntityId().getId()).add((to.getEntityId().getId() << 3) | (long) type);
                    }
                }
            }
        }
    }
}
Also used : Relation(net.osmand.osm.edit.Relation) RelationMember(net.osmand.osm.edit.Relation.RelationMember) TLongArrayList(gnu.trove.list.array.TLongArrayList)

Aggregations

Relation (net.osmand.osm.edit.Relation)30 Way (net.osmand.osm.edit.Way)17 RelationMember (net.osmand.osm.edit.Relation.RelationMember)16 Entity (net.osmand.osm.edit.Entity)15 Node (net.osmand.osm.edit.Node)14 EntityId (net.osmand.osm.edit.Entity.EntityId)11 ArrayList (java.util.ArrayList)8 LinkedHashMap (java.util.LinkedHashMap)7 TLongArrayList (gnu.trove.list.array.TLongArrayList)4 IOException (java.io.IOException)4 LinkedHashSet (java.util.LinkedHashSet)4 EntityInfo (net.osmand.osm.edit.EntityInfo)4 File (java.io.File)3 SQLException (java.sql.SQLException)3 HashMap (java.util.HashMap)3 LatLon (net.osmand.data.LatLon)3 EntityType (net.osmand.osm.edit.Entity.EntityType)3 FileOutputStream (java.io.FileOutputStream)2 ResultSet (java.sql.ResultSet)2 List (java.util.List)2