Search in sources :

Example 6 with BinaryMapIndexReader

use of net.osmand.binary.BinaryMapIndexReader in project OsmAnd-tools by osmandapp.

the class MapAddressLayer method justifyResults.

private double justifyResults(List<BinaryMapIndexReader> list, GeocodingUtilities su, List<GeocodingResult> complete, List<GeocodingResult> res) throws IOException {
    double minBuildingDistance = 0;
    for (GeocodingResult r : res) {
        BinaryMapIndexReader reader = null;
        for (BinaryMapIndexReader b : list) {
            for (RouteRegion rb : b.getRoutingIndexes()) {
                if (r.regionFP == rb.getFilePointer() && r.regionLen == rb.getLength()) {
                    reader = b;
                    break;
                }
            }
            if (reader != null) {
                break;
            }
        }
        if (reader != null) {
            List<GeocodingResult> justified = su.justifyReverseGeocodingSearch(r, reader, minBuildingDistance, null);
            if (!justified.isEmpty()) {
                double md = justified.get(0).getDistance();
                if (minBuildingDistance == 0) {
                    minBuildingDistance = md;
                } else {
                    minBuildingDistance = Math.min(md, minBuildingDistance);
                }
                complete.addAll(justified);
            }
        } else {
            complete.add(r);
        }
    }
    return minBuildingDistance;
}
Also used : GeocodingResult(net.osmand.binary.GeocodingUtilities.GeocodingResult) RouteRegion(net.osmand.binary.BinaryMapRouteReaderAdapter.RouteRegion) BinaryMapIndexReader(net.osmand.binary.BinaryMapIndexReader)

Example 7 with BinaryMapIndexReader

use of net.osmand.binary.BinaryMapIndexReader in project OsmAnd-tools by osmandapp.

the class MapAddressLayer method whereAmI.

private List<Entity> whereAmI(double lat, double lon, final DataTileManager<Entity> points) throws IOException {
    List<Entity> results = new ArrayList<Entity>();
    int x = MapUtils.get31TileNumberX(lon);
    int y = MapUtils.get31TileNumberY(lat);
    List<BinaryMapIndexReader> list = new ArrayList<BinaryMapIndexReader>();
    for (File f : new File(DataExtractionSettings.getSettings().getBinaryFilesDir()).listFiles()) {
        if (f.getName().endsWith(".obf")) {
            // $NON-NLS-1$ //$NON-NLS-2$
            RandomAccessFile raf = new RandomAccessFile(f, "r");
            BinaryMapIndexReader rd = new BinaryMapIndexReader(raf, f);
            if (rd.containsAddressData() && rd.containsRouteData(x, y, x, y, 15)) {
                list.add(rd);
            } else {
                rd.close();
                raf.close();
            }
        }
    }
    RoutingConfiguration cfg = DataExtractionSettings.getSettings().getRoutingConfig().build("geocoding", 100, new HashMap<String, String>());
    RoutingContext ctx = new RoutePlannerFrontEnd(false).buildRoutingContext(cfg, null, list.toArray(new BinaryMapIndexReader[list.size()]));
    GeocodingUtilities su = new GeocodingUtilities();
    double minBuildingDistance = 0;
    List<GeocodingResult> complete = new ArrayList<GeocodingUtilities.GeocodingResult>();
    List<GeocodingResult> res = su.reverseGeocodingSearch(ctx, lat, lon, false);
    minBuildingDistance = justifyResults(list, su, complete, res);
    // complete.addAll(res);
    Collections.sort(complete, GeocodingUtilities.DISTANCE_COMPARATOR);
    long lid = -1;
    for (GeocodingResult r : complete) {
        if (r.building != null && r.getDistance() > minBuildingDistance * GeocodingUtilities.THRESHOLD_MULTIPLIER_SKIP_BUILDINGS_AFTER) {
            continue;
        }
        Node n = new Node(r.getLocation().getLatitude(), r.getLocation().getLongitude(), lid--);
        n.putTag(OSMTagKey.NAME.getValue(), r.toString());
        results.add(n);
    }
    for (BinaryMapIndexReader l : list) {
        l.close();
    }
    return results;
}
Also used : Entity(net.osmand.osm.edit.Entity) GeocodingResult(net.osmand.binary.GeocodingUtilities.GeocodingResult) Node(net.osmand.osm.edit.Node) ArrayList(java.util.ArrayList) BinaryMapIndexReader(net.osmand.binary.BinaryMapIndexReader) Point(java.awt.Point) RoutingContext(net.osmand.router.RoutingContext) RandomAccessFile(java.io.RandomAccessFile) RoutingConfiguration(net.osmand.router.RoutingConfiguration) RoutePlannerFrontEnd(net.osmand.router.RoutePlannerFrontEnd) GeocodingUtilities(net.osmand.binary.GeocodingUtilities) RandomAccessFile(java.io.RandomAccessFile) File(java.io.File)

Example 8 with BinaryMapIndexReader

use of net.osmand.binary.BinaryMapIndexReader in project OsmAnd-tools by osmandapp.

the class ObfFileInMemory method readObfFiles.

public void readObfFiles(List<File> files) throws IOException {
    for (int i = 0; i < files.size(); i++) {
        File inputFile = files.get(i);
        File nonGzip = inputFile;
        boolean gzip = false;
        if (inputFile.getName().endsWith(".gz")) {
            nonGzip = new File(inputFile.getParentFile(), inputFile.getName().substring(0, inputFile.getName().length() - 3));
            GZIPInputStream gzin = new GZIPInputStream(new FileInputStream(inputFile));
            FileOutputStream fous = new FileOutputStream(nonGzip);
            Algorithms.streamCopy(gzin, fous);
            fous.close();
            gzin.close();
            gzip = true;
        }
        RandomAccessFile raf = new RandomAccessFile(nonGzip, "r");
        BinaryMapIndexReader indexReader = new BinaryMapIndexReader(raf, nonGzip);
        for (BinaryIndexPart p : indexReader.getIndexes()) {
            if (p instanceof MapIndex) {
                MapIndex mi = (MapIndex) p;
                for (MapRoot mr : mi.getRoots()) {
                    MapZooms.MapZoomPair pair = new MapZooms.MapZoomPair(mr.getMinZoom(), mr.getMaxZoom());
                    TLongObjectHashMap<BinaryMapDataObject> objects = readBinaryMapData(indexReader, mi, mr.getMinZoom());
                    putMapObjects(pair, objects.valueCollection(), true);
                }
            } else if (p instanceof RouteRegion) {
                RouteRegion rr = (RouteRegion) p;
                readRoutingData(indexReader, rr, ZOOM_LEVEL_ROUTING, true);
            } else if (p instanceof PoiRegion) {
                PoiRegion pr = (PoiRegion) p;
                TLongObjectHashMap<Map<String, Amenity>> rr = readPoiData(indexReader, pr, ZOOM_LEVEL_POI, true);
                putPoiData(rr, true);
            } else if (p instanceof TransportIndex) {
            // read all data later
            }
        }
        readTransportData(indexReader, true);
        updateTimestamp(indexReader.getDateCreated());
        indexReader.close();
        raf.close();
        if (gzip) {
            nonGzip.delete();
        }
    }
}
Also used : MapZoomPair(net.osmand.binary.MapZooms.MapZoomPair) BinaryIndexPart(net.osmand.binary.BinaryIndexPart) BinaryMapIndexReader(net.osmand.binary.BinaryMapIndexReader) MapRoot(net.osmand.binary.BinaryMapIndexReader.MapRoot) FileInputStream(java.io.FileInputStream) GZIPInputStream(java.util.zip.GZIPInputStream) MapZoomPair(net.osmand.binary.MapZooms.MapZoomPair) RandomAccessFile(java.io.RandomAccessFile) BinaryMapDataObject(net.osmand.binary.BinaryMapDataObject) FileOutputStream(java.io.FileOutputStream) RouteRegion(net.osmand.binary.BinaryMapRouteReaderAdapter.RouteRegion) MapZooms(net.osmand.binary.MapZooms) MapIndex(net.osmand.binary.BinaryMapIndexReader.MapIndex) TransportIndex(net.osmand.binary.BinaryMapTransportReaderAdapter.TransportIndex) RandomAccessFile(java.io.RandomAccessFile) File(java.io.File) Map(java.util.Map) LinkedHashMap(java.util.LinkedHashMap) TLongObjectHashMap(gnu.trove.map.hash.TLongObjectHashMap) TreeMap(java.util.TreeMap) PoiRegion(net.osmand.binary.BinaryMapPoiReaderAdapter.PoiRegion)

Example 9 with BinaryMapIndexReader

use of net.osmand.binary.BinaryMapIndexReader in project OsmAnd-tools by osmandapp.

the class BinaryMerger method combineParts.

public void combineParts(File fileToExtract, List<File> files, Set<Integer> combineParts) throws IOException, SQLException {
    BinaryMapIndexReader[] indexes = new BinaryMapIndexReader[files.size()];
    RandomAccessFile[] rafs = new RandomAccessFile[files.size()];
    long dateCreated = 0;
    int version = -1;
    // Go through all files and validate consistency
    int c = 0;
    for (File f : files) {
        if (f.getAbsolutePath().equals(fileToExtract.getAbsolutePath())) {
            System.err.println("Error : Input file is equal to output file " + f.getAbsolutePath());
            return;
        }
        rafs[c] = new RandomAccessFile(f.getAbsolutePath(), "r");
        indexes[c] = new BinaryMapIndexReader(rafs[c], f);
        dateCreated = Math.max(dateCreated, indexes[c].getDateCreated());
        if (version == -1) {
            version = indexes[c].getVersion();
        } else {
            if (indexes[c].getVersion() != version) {
                System.err.println("Error : Different input files has different input versions " + indexes[c].getVersion() + " != " + version);
                return;
            }
        }
        c++;
    }
    // write files
    RandomAccessFile rafToExtract = new RandomAccessFile(fileToExtract, "rw");
    BinaryMapIndexWriter writer = new BinaryMapIndexWriter(rafToExtract, dateCreated);
    CodedOutputStream ous = writer.getCodedOutStream();
    byte[] BUFFER_TO_READ = new byte[BUFFER_SIZE];
    AddressRegion[] addressRegions = new AddressRegion[files.size()];
    PoiRegion[] poiRegions = new PoiRegion[files.size()];
    for (int k = 0; k < indexes.length; k++) {
        BinaryMapIndexReader index = indexes[k];
        RandomAccessFile raf = rafs[k];
        for (int i = 0; i < index.getIndexes().size(); i++) {
            BinaryIndexPart part = index.getIndexes().get(i);
            if (combineParts.contains(part.getFieldNumber())) {
                if (part.getFieldNumber() == OsmandOdb.OsmAndStructure.ADDRESSINDEX_FIELD_NUMBER) {
                    addressRegions[k] = (AddressRegion) part;
                } else if (part.getFieldNumber() == OsmandOdb.OsmAndStructure.POIINDEX_FIELD_NUMBER) {
                    poiRegions[k] = (PoiRegion) part;
                }
            } else {
                ous.writeTag(part.getFieldNumber(), WireFormat.WIRETYPE_FIXED32_LENGTH_DELIMITED);
                writeInt(ous, part.getLength());
                copyBinaryPart(ous, BUFFER_TO_READ, raf, part.getFilePointer(), part.getLength());
                System.out.println(MessageFormat.format("{2} part {0} is extracted {1} bytes", new Object[] { part.getName(), part.getLength(), part.getPartName() }));
            }
        }
    }
    String nm = fileToExtract.getName();
    int i = nm.indexOf('_');
    if (i > 0) {
        nm = nm.substring(0, i);
    }
    if (combineParts.contains(OsmandOdb.OsmAndStructure.ADDRESSINDEX_FIELD_NUMBER)) {
        combineAddressIndex(nm, writer, addressRegions, indexes);
    }
    if (combineParts.contains(OsmandOdb.OsmAndStructure.POIINDEX_FIELD_NUMBER)) {
        combinePoiIndex(nm, writer, dateCreated, poiRegions, indexes);
    }
    ous.writeInt32(OsmandOdb.OsmAndStructure.VERSIONCONFIRM_FIELD_NUMBER, version);
    ous.flush();
}
Also used : BinaryIndexPart(net.osmand.binary.BinaryIndexPart) CodedOutputStream(com.google.protobuf.CodedOutputStream) BinaryMapIndexReader(net.osmand.binary.BinaryMapIndexReader) RandomAccessFile(java.io.RandomAccessFile) MapObject(net.osmand.data.MapObject) BinaryMapIndexWriter(net.osmand.data.preparation.BinaryMapIndexWriter) AddressRegion(net.osmand.binary.BinaryMapAddressReaderAdapter.AddressRegion) RandomAccessFile(java.io.RandomAccessFile) File(java.io.File) PoiRegion(net.osmand.binary.BinaryMapPoiReaderAdapter.PoiRegion)

Example 10 with BinaryMapIndexReader

use of net.osmand.binary.BinaryMapIndexReader in project OsmAnd-tools by osmandapp.

the class BinaryMerger method combinePoiIndex.

private void combinePoiIndex(String name, BinaryMapIndexWriter writer, long dateCreated, PoiRegion[] poiRegions, BinaryMapIndexReader[] indexes) throws IOException, SQLException {
    final int[] writtenPoiCount = { 0 };
    MapRenderingTypesEncoder renderingTypes = new MapRenderingTypesEncoder(null, name);
    boolean overwriteIds = false;
    final IndexPoiCreator indexPoiCreator = new IndexPoiCreator(renderingTypes, overwriteIds);
    indexPoiCreator.createDatabaseStructure(new File(new File(System.getProperty("user.dir")), IndexCreator.getPoiFileName(name)));
    final Map<Long, List<Amenity>> amenityRelations = new HashMap<Long, List<Amenity>>();
    final TLongHashSet set = new TLongHashSet();
    final long[] generatedRelationId = { -1 };
    for (int i = 0; i < poiRegions.length; i++) {
        BinaryMapIndexReader index = indexes[i];
        final TLongHashSet file = new TLongHashSet();
        log.info("Region: " + extractRegionName(index));
        index.searchPoi(BinaryMapIndexReader.buildSearchPoiRequest(0, Integer.MAX_VALUE, 0, Integer.MAX_VALUE, -1, BinaryMapIndexReader.ACCEPT_ALL_POI_TYPE_FILTER, new ResultMatcher<Amenity>() {

            @Override
            public boolean publish(Amenity amenity) {
                try {
                    boolean isRelation = amenity.getId() < 0;
                    if (isRelation) {
                        long j = latlon(amenity);
                        List<Amenity> list;
                        if (!amenityRelations.containsKey(j)) {
                            list = new ArrayList<Amenity>(1);
                            amenityRelations.put(j, list);
                        } else {
                            list = amenityRelations.get(j);
                        }
                        boolean unique = true;
                        for (Amenity a : list) {
                            if (a.getType() == amenity.getType() && Algorithms.objectEquals(a.getSubType(), amenity.getSubType())) {
                                unique = false;
                                break;
                            }
                        }
                        if (unique) {
                            amenity.setId(generatedRelationId[0]--);
                            amenityRelations.get(j).add(amenity);
                            indexPoiCreator.insertAmenityIntoPoi(amenity);
                            writtenPoiCount[0]++;
                        }
                    } else {
                        if (!set.contains(amenity.getId())) {
                            file.add(amenity.getId());
                            indexPoiCreator.insertAmenityIntoPoi(amenity);
                            writtenPoiCount[0]++;
                        }
                    }
                    return false;
                } catch (SQLException e) {
                    throw new RuntimeException(e);
                }
            }

            @Override
            public boolean isCancelled() {
                return false;
            }
        }));
        set.addAll(file);
    }
    indexPoiCreator.writeBinaryPoiIndex(writer, name, null);
    indexPoiCreator.commitAndClosePoiFile(dateCreated);
    // REMOVE_POI_DB = false;
    if (REMOVE_POI_DB) {
        indexPoiCreator.removePoiFile();
    }
    log.info("Written " + writtenPoiCount[0] + " POI.");
}
Also used : Amenity(net.osmand.data.Amenity) HashMap(java.util.HashMap) LinkedHashMap(java.util.LinkedHashMap) SQLException(java.sql.SQLException) BinaryMapIndexReader(net.osmand.binary.BinaryMapIndexReader) IndexPoiCreator(net.osmand.data.preparation.IndexPoiCreator) MapRenderingTypesEncoder(net.osmand.osm.MapRenderingTypesEncoder) TLongHashSet(gnu.trove.set.hash.TLongHashSet) List(java.util.List) ArrayList(java.util.ArrayList) RandomAccessFile(java.io.RandomAccessFile) File(java.io.File)

Aggregations

BinaryMapIndexReader (net.osmand.binary.BinaryMapIndexReader)48 File (java.io.File)26 RandomAccessFile (java.io.RandomAccessFile)25 ArrayList (java.util.ArrayList)24 IOException (java.io.IOException)16 List (java.util.List)7 RoutePlannerFrontEnd (net.osmand.router.RoutePlannerFrontEnd)7 RoutingConfiguration (net.osmand.router.RoutingConfiguration)7 LinkedHashMap (java.util.LinkedHashMap)6 LatLon (net.osmand.data.LatLon)6 HashMap (java.util.HashMap)5 BinaryMapDataObject (net.osmand.binary.BinaryMapDataObject)5 MapIndex (net.osmand.binary.BinaryMapIndexReader.MapIndex)5 RouteRegion (net.osmand.binary.BinaryMapRouteReaderAdapter.RouteRegion)5 City (net.osmand.data.City)5 RoutingContext (net.osmand.router.RoutingContext)5 Point (java.awt.Point)4 FileOutputStream (java.io.FileOutputStream)4 GeocodingResult (net.osmand.binary.GeocodingUtilities.GeocodingResult)4 TLongHashSet (gnu.trove.set.hash.TLongHashSet)3