Search in sources :

Example 21 with Int2ObjectOpenHashMap

use of it.unimi.dsi.fastutil.ints.Int2ObjectOpenHashMap in project mkgmap by openstreetmap.

the class HousenumberGenerator method generate.

/**
 * @param adder
 * @param naxNodeId the highest nodeId used before
 */
public void generate(LineAdder adder, int naxNodeId) {
    if (numbersEnabled) {
        MultiHashMap<MapRoad, HousenumberMatch> initialHousesForRoads = findClosestRoadsToHouse();
        identifyServiceRoads();
        handleInterpolationWays(initialHousesForRoads);
        List<HousenumberRoad> hnrList = createHousenumberRoads(initialHousesForRoads);
        initialHousesForRoads = null;
        log.info("found", hnrList.size(), "road candidates for address search");
        useAddrPlaceTag(hnrList);
        Map<MapRoad, HousenumberRoad> road2HousenumberRoadMap = new HashMap<>();
        for (HousenumberRoad hnr : hnrList) {
            road2HousenumberRoadMap.put(hnr.getRoad(), hnr);
        }
        Int2ObjectOpenHashMap<HashSet<MapRoad>> nodeId2RoadLists = new Int2ObjectOpenHashMap<>();
        for (MapRoad road : allRoads) {
            for (Coord co : road.getPoints()) {
                if (co.getId() == 0)
                    continue;
                HashSet<MapRoad> connectedRoads = nodeId2RoadLists.get(co.getId());
                if (connectedRoads == null) {
                    connectedRoads = new HashSet<>();
                    nodeId2RoadLists.put(co.getId(), connectedRoads);
                }
                connectedRoads.add(road);
            }
        }
        List<HousenumberRoad> addedRoads = new ArrayList<>();
        Iterator<HousenumberRoad> iter = hnrList.iterator();
        while (iter.hasNext()) {
            HousenumberRoad hnr = iter.next();
            List<HousenumberMatch> lostHouses = hnr.checkStreetName(road2HousenumberRoadMap, nodeId2RoadLists);
            for (HousenumberMatch house : lostHouses) {
                MapRoad r = house.getRoad();
                if (r != null) {
                    HousenumberRoad hnr2 = road2HousenumberRoadMap.get(r);
                    if (hnr2 == null) {
                        CityInfo ci = getCityInfos(r.getCity(), r.getRegion(), r.getCountry());
                        hnr2 = new HousenumberRoad(r, ci, Arrays.asList(house));
                        if (r.getZip() != null)
                            hnr2.setZipCodeInfo(getZipInfos(r.getZip()));
                        road2HousenumberRoadMap.put(r, hnr2);
                        addedRoads.add(hnr2);
                    } else {
                        hnr2.addHouse(house);
                    }
                }
            }
            if (hnr.getName() == null) {
                iter.remove();
                for (HousenumberMatch house : hnr.getHouses()) {
                    log.warn("found no plausible road name for address", house.toBrowseURL(), ", closest road id:", house.getRoad());
                }
            }
        }
        hnrList.addAll(addedRoads);
        // TODO: interpolate addr:interpolation houses
        removeDupsGroupedByCityAndName(hnrList);
        // group by street name and city
        TreeMap<String, TreeMap<CityInfo, List<HousenumberRoad>>> streetnameCityRoadMap = new TreeMap<>();
        for (HousenumberRoad hnr : hnrList) {
            TreeMap<CityInfo, List<HousenumberRoad>> cluster = streetnameCityRoadMap.get(hnr.getName());
            if (cluster == null) {
                cluster = new TreeMap<>();
                streetnameCityRoadMap.put(hnr.getName(), cluster);
            }
            List<HousenumberRoad> roadsInCluster = cluster.get(hnr.getRoadCityInfo());
            if (roadsInCluster == null) {
                roadsInCluster = new ArrayList<>();
                cluster.put(hnr.getRoadCityInfo(), roadsInCluster);
            }
            roadsInCluster.add(hnr);
        }
        for (Entry<String, TreeMap<CityInfo, List<HousenumberRoad>>> streetNameEntry : streetnameCityRoadMap.entrySet()) {
            String streetName = streetNameEntry.getKey();
            for (Entry<CityInfo, List<HousenumberRoad>> clusterEntry : streetNameEntry.getValue().entrySet()) {
                useInterpolationInfo(streetName, clusterEntry.getValue(), road2HousenumberRoadMap);
            }
            for (Entry<CityInfo, List<HousenumberRoad>> clusterEntry : streetNameEntry.getValue().entrySet()) {
                List<HousenumberRoad> roadsInCluster = clusterEntry.getValue();
                if (log.isDebugEnabled()) {
                    log.debug("processing road(s) with name", streetName, "in", clusterEntry.getKey());
                }
                for (HousenumberRoad hnr : roadsInCluster) {
                    hnr.buildIntervals();
                }
                boolean optimized = false;
                for (int loop = 0; loop < 10; loop++) {
                    for (HousenumberRoad hnr : roadsInCluster) {
                        hnr.checkIntervals();
                    }
                    checkWrongRoadAssignmments(roadsInCluster);
                    boolean changed = hasChanges(roadsInCluster);
                    if (!optimized && !changed) {
                        for (HousenumberRoad hnr : roadsInCluster) {
                            hnr.improveSearchResults();
                        }
                        changed = hasChanges(roadsInCluster);
                        optimized = true;
                    }
                    if (!changed)
                        break;
                }
                for (HousenumberRoad hnr : roadsInCluster) {
                    hnr.setNumbers();
                }
            }
        }
    }
    if (log.isInfoEnabled()) {
        for (HousenumberElem house : houseElems) {
            if (house.getRoad() == null) {
                if (house.getStreet() != null)
                    log.info("found no plausible road for house number element", house.toBrowseURL(), house.getStreet(), house.getSign());
                else
                    log.info("found no plausible road for house number element", house.toBrowseURL());
            }
        }
    }
    for (MapRoad r : allRoads) {
        if (log.isDebugEnabled()) {
            List<Numbers> finalNumbers = r.getRoadDef().getNumbersList();
            if (finalNumbers != null) {
                log.info("id:" + r.getRoadDef().getId(), ", final numbers,", r, "in", r.getCity());
                for (Numbers cn : finalNumbers) {
                    if (cn.isEmpty())
                        continue;
                    log.info("id:" + r.getRoadDef().getId(), ", Left: ", cn.getLeftNumberStyle(), cn.getIndex(), "Start:", cn.getLeftStart(), "End:", cn.getLeftEnd());
                    log.info("id:" + r.getRoadDef().getId(), ", Right:", cn.getRightNumberStyle(), cn.getIndex(), "Start:", cn.getRightStart(), "End:", cn.getRightEnd());
                }
            }
        }
        adder.add(r);
    }
}
Also used : CityInfo(uk.me.parabola.mkgmap.general.CityInfo) HashMap(java.util.HashMap) LinkedHashMap(java.util.LinkedHashMap) Long2ObjectOpenHashMap(it.unimi.dsi.fastutil.longs.Long2ObjectOpenHashMap) Int2ObjectOpenHashMap(it.unimi.dsi.fastutil.ints.Int2ObjectOpenHashMap) Int2IntOpenHashMap(it.unimi.dsi.fastutil.ints.Int2IntOpenHashMap) MultiHashMap(uk.me.parabola.util.MultiHashMap) LongArrayList(it.unimi.dsi.fastutil.longs.LongArrayList) ArrayList(java.util.ArrayList) Numbers(uk.me.parabola.imgfmt.app.net.Numbers) LongArrayList(it.unimi.dsi.fastutil.longs.LongArrayList) ArrayList(java.util.ArrayList) List(java.util.List) HashSet(java.util.HashSet) LinkedHashSet(java.util.LinkedHashSet) Int2ObjectOpenHashMap(it.unimi.dsi.fastutil.ints.Int2ObjectOpenHashMap) MapRoad(uk.me.parabola.mkgmap.general.MapRoad) TreeMap(java.util.TreeMap) Coord(uk.me.parabola.imgfmt.app.Coord)

Example 22 with Int2ObjectOpenHashMap

use of it.unimi.dsi.fastutil.ints.Int2ObjectOpenHashMap in project mkgmap by openstreetmap.

the class HousenumberRoad method checkStreetName.

/**
 * Check if street name is set, if not, try to find one.
 * Identify those houses which are assigned to this road because it was the closest,
 * but can't be correct because street name doesn't match.
 *
 * @param road2HousenumberRoadMap maps {@link MapRoad} instances to corresponding
 * {@link HousenumberRoad} instances
 * @param nodeId2RoadLists maps node ids to the {@link MapRoad} that use the corresponding nodes.
 * @return
 */
public List<HousenumberMatch> checkStreetName(Map<MapRoad, HousenumberRoad> road2HousenumberRoadMap, Int2ObjectOpenHashMap<HashSet<MapRoad>> nodeId2RoadLists) {
    List<HousenumberMatch> noWrongHouses = Collections.emptyList();
    List<HousenumberMatch> wrongHouses = Collections.emptyList();
    double minDist = Double.MAX_VALUE;
    double maxDist = 0;
    if (houseNumbers.isEmpty() == false) {
        HashMap<String, Integer> possibleStreetNamesFromHouses = new HashMap<>();
        HashMap<String, Integer> possiblePlaceNamesFromHouses = new HashMap<>();
        for (HousenumberMatch house : houseNumbers) {
            if (house.getDistance() > maxDist)
                maxDist = house.getDistance();
            if (house.getDistance() < minDist)
                minDist = house.getDistance();
            String potentialName = house.getStreet();
            if (potentialName != null) {
                Integer oldCount = possibleStreetNamesFromHouses.put(potentialName, 1);
                if (oldCount != null)
                    possibleStreetNamesFromHouses.put(potentialName, oldCount + 1);
            }
            String placeName = house.getPlace();
            if (placeName != null) {
                Integer oldCount = possiblePlaceNamesFromHouses.put(placeName, 1);
                if (oldCount != null)
                    possiblePlaceNamesFromHouses.put(placeName, oldCount + 1);
            }
        }
        HashSet<String> connectedRoadNames = new HashSet<>();
        for (Coord co : road.getPoints()) {
            if (co.getId() == 0)
                continue;
            HashSet<MapRoad> connectedRoads = nodeId2RoadLists.get(co.getId());
            for (MapRoad r : connectedRoads) {
                if (r.getStreet() != null)
                    connectedRoadNames.add(r.getStreet());
            }
        }
        if (streetName != null) {
            if (possibleStreetNamesFromHouses.isEmpty()) {
                // ok, houses have no street name
                return noWrongHouses;
            }
            if (possibleStreetNamesFromHouses.size() == 1) {
                if (possibleStreetNamesFromHouses.containsKey(streetName)) {
                    // ok, houses have same name as street
                    return noWrongHouses;
                }
            }
        }
        if (possibleStreetNamesFromHouses.isEmpty()) {
            // neither road not houses tell us a street name
            if (furtherNames != null && furtherNames.size() > 0) {
                Iterator<String> iter = furtherNames.iterator();
                streetName = iter.next();
                iter.remove();
                if (furtherNames.isEmpty())
                    furtherNames = null;
            }
            return noWrongHouses;
        }
        if (streetName == null) {
            if (possibleStreetNamesFromHouses.size() == 1) {
                String potentialName = possibleStreetNamesFromHouses.keySet().iterator().next();
                boolean nameOK = false;
                if (connectedRoadNames.contains(potentialName))
                    nameOK = true;
                else if (houseNumbers.size() > 1) {
                    nameOK = true;
                } else if (maxDist <= 10) {
                    nameOK = true;
                }
                if (nameOK) {
                    streetName = potentialName;
                    // all good, return empty list
                    return noWrongHouses;
                }
            } else {
                List<String> matchingNames = new ArrayList<>();
                for (Entry<String, Integer> entry : possibleStreetNamesFromHouses.entrySet()) {
                    String name = entry.getKey();
                    if (connectedRoadNames.contains(name)) {
                        matchingNames.add(name);
                    }
                }
                if (matchingNames.size() == 1) {
                    streetName = matchingNames.get(0);
                }
            }
        }
        // if we get here we have no usable street name
        wrongHouses = new ArrayList<>();
        Iterator<HousenumberMatch> iter = houseNumbers.iterator();
        while (iter.hasNext()) {
            HousenumberMatch house = iter.next();
            if (streetName != null) {
                if (house.getStreet() == null || streetName.equalsIgnoreCase(house.getStreet()))
                    continue;
            } else if (house.getPlace() != null)
                continue;
            double bestDist = Double.MAX_VALUE;
            HousenumberMatch best = null;
            for (MapRoad altRoad : house.getAlternativeRoads()) {
                if (house.getStreet() != null) {
                    if (house.getStreet().equals(altRoad.getStreet())) {
                        HousenumberMatch test = new HousenumberMatch(house);
                        HousenumberGenerator.findClosestRoadSegment(test, altRoad);
                        if (test.getDistance() < bestDist) {
                            best = test;
                            bestDist = test.getDistance();
                        }
                    }
                }
            }
            iter.remove();
            if (best != null) {
                best.calcRoadSide();
                wrongHouses.add(best);
            } else {
                log.warn("found no plausible road for address", house.getStreet(), house, house.toBrowseURL());
            }
        }
    }
    return wrongHouses;
}
Also used : Int2ObjectOpenHashMap(it.unimi.dsi.fastutil.ints.Int2ObjectOpenHashMap) HashMap(java.util.HashMap) ArrayList(java.util.ArrayList) MapRoad(uk.me.parabola.mkgmap.general.MapRoad) Coord(uk.me.parabola.imgfmt.app.Coord) HashSet(java.util.HashSet) LinkedHashSet(java.util.LinkedHashSet)

Example 23 with Int2ObjectOpenHashMap

use of it.unimi.dsi.fastutil.ints.Int2ObjectOpenHashMap in project mule by mulesoft.

the class LoggerContextCache method doGetLoggerContext.

/**
 * The {@link Callable} passed to the Guava cache, because
 *
 * Guava cache will use its logging framework to log something, and that logger will end up calling here.
 * <p>
 * With the check in the {@link Callable} passed to the Guava cache, we avoid building an extra context. We cannot just use a
 * map, because it may result in an eternal recurrent call, Guava does a good job at handling that situation. It is just the
 * logging that Guava tries to do that may disrupt thing when initializing the logging infrastructure.
 *
 * @param classLoader
 * @param key
 * @return
 * @throws ExecutionException
 */
protected LoggerContext doGetLoggerContext(final ClassLoader classLoader, final Integer key) throws ExecutionException {
    return activeContexts.get(key, () -> {
        if (builtContexts.containsKey(key.intValue())) {
            return builtContexts.get(key.intValue());
        } else {
            LoggerContext context = artifactAwareContextSelector.buildContext(classLoader);
            Int2ObjectMap<LoggerContext> newBuiltContexts = new Int2ObjectOpenHashMap<>(builtContexts);
            newBuiltContexts.put(key.intValue(), context);
            builtContexts = newBuiltContexts;
            return context;
        }
    });
}
Also used : Int2ObjectOpenHashMap(it.unimi.dsi.fastutil.ints.Int2ObjectOpenHashMap) LoggerContext(org.apache.logging.log4j.core.LoggerContext)

Example 24 with Int2ObjectOpenHashMap

use of it.unimi.dsi.fastutil.ints.Int2ObjectOpenHashMap in project LanternServer by LanternPowered.

the class InventorySnapshot method ofInventory.

/**
 * Creates a {@link InventorySnapshot} for the specified {@link Inventory}.
 *
 * @param inventory The inventory
 * @return The snapshot
 */
public static InventorySnapshot ofInventory(Inventory inventory) {
    final Iterator<Slot> it = inventory.<Slot>slots().iterator();
    final Int2ObjectMap<ItemStackSnapshot> itemStackSnapshots = new Int2ObjectOpenHashMap<>();
    while (it.hasNext()) {
        final Slot slot = it.next();
        slot.peek().map(ItemStack::createSnapshot).ifPresent(itemStackSnapshot -> {
            // noinspection ConstantConditions
            final SlotIndex index = inventory.getProperty(slot, SlotIndex.class, null).get();
            itemStackSnapshots.put(index.getValue(), itemStackSnapshot);
        });
    }
    return new InventorySnapshot(itemStackSnapshots);
}
Also used : Int2ObjectOpenHashMap(it.unimi.dsi.fastutil.ints.Int2ObjectOpenHashMap) SlotIndex(org.spongepowered.api.item.inventory.property.SlotIndex) Slot(org.spongepowered.api.item.inventory.Slot) ItemStackSnapshot(org.spongepowered.api.item.inventory.ItemStackSnapshot)

Example 25 with Int2ObjectOpenHashMap

use of it.unimi.dsi.fastutil.ints.Int2ObjectOpenHashMap in project druid by druid-io.

the class PartialSegmentMergeTask method fetchSegmentFiles.

private Map<Interval, Int2ObjectMap<List<File>>> fetchSegmentFiles(TaskToolbox toolbox, Map<Interval, Int2ObjectMap<List<PartitionLocation>>> intervalToBuckets) throws IOException {
    final File tempDir = toolbox.getIndexingTmpDir();
    org.apache.commons.io.FileUtils.deleteQuietly(tempDir);
    FileUtils.mkdirp(tempDir);
    final Map<Interval, Int2ObjectMap<List<File>>> intervalToUnzippedFiles = new HashMap<>();
    // Fetch partition files
    for (Entry<Interval, Int2ObjectMap<List<PartitionLocation>>> entryPerInterval : intervalToBuckets.entrySet()) {
        final Interval interval = entryPerInterval.getKey();
        for (Int2ObjectMap.Entry<List<PartitionLocation>> entryPerBucketId : entryPerInterval.getValue().int2ObjectEntrySet()) {
            final int bucketId = entryPerBucketId.getIntKey();
            final File partitionDir = org.apache.commons.io.FileUtils.getFile(tempDir, interval.getStart().toString(), interval.getEnd().toString(), Integer.toString(bucketId));
            FileUtils.mkdirp(partitionDir);
            for (PartitionLocation location : entryPerBucketId.getValue()) {
                final File unzippedDir = toolbox.getShuffleClient().fetchSegmentFile(partitionDir, supervisorTaskId, location);
                intervalToUnzippedFiles.computeIfAbsent(interval, k -> new Int2ObjectOpenHashMap<>()).computeIfAbsent(bucketId, k -> new ArrayList<>()).add(unzippedDir);
            }
        }
    }
    return intervalToUnzippedFiles;
}
Also used : TaskToolbox(org.apache.druid.indexing.common.TaskToolbox) LockListAction(org.apache.druid.indexing.common.actions.LockListAction) Logger(org.apache.druid.java.util.common.logger.Logger) JsonProperty(com.fasterxml.jackson.annotation.JsonProperty) DataSegmentPusher(org.apache.druid.segment.loading.DataSegmentPusher) Arrays(java.util.Arrays) Stopwatch(com.google.common.base.Stopwatch) HashMap(java.util.HashMap) TaskResource(org.apache.druid.indexing.common.task.TaskResource) TaskStatus(org.apache.druid.indexer.TaskStatus) Pair(org.apache.druid.java.util.common.Pair) ArrayList(java.util.ArrayList) HashSet(java.util.HashSet) BaseProgressIndicator(org.apache.druid.segment.BaseProgressIndicator) Interval(org.joda.time.Interval) TaskActionClient(org.apache.druid.indexing.common.actions.TaskActionClient) Map(java.util.Map) TaskLock(org.apache.druid.indexing.common.TaskLock) RetryUtils(org.apache.druid.java.util.common.RetryUtils) IndexMergerV9(org.apache.druid.segment.IndexMergerV9) FileUtils(org.apache.druid.java.util.common.FileUtils) Nullable(javax.annotation.Nullable) ShardSpec(org.apache.druid.timeline.partition.ShardSpec) Int2ObjectOpenHashMap(it.unimi.dsi.fastutil.ints.Int2ObjectOpenHashMap) ImmutableMap(com.google.common.collect.ImmutableMap) ClientBasedTaskInfoProvider(org.apache.druid.indexing.common.task.ClientBasedTaskInfoProvider) IndexMerger(org.apache.druid.segment.IndexMerger) Closer(org.apache.druid.java.util.common.io.Closer) AggregatorFactory(org.apache.druid.query.aggregation.AggregatorFactory) QueryableIndex(org.apache.druid.segment.QueryableIndex) StringUtils(org.apache.druid.java.util.common.StringUtils) Set(java.util.Set) ISE(org.apache.druid.java.util.common.ISE) IOException(java.io.IOException) Maps(com.google.common.collect.Maps) Collectors(java.util.stream.Collectors) File(java.io.File) TimeUnit(java.util.concurrent.TimeUnit) List(java.util.List) Int2ObjectMap(it.unimi.dsi.fastutil.ints.Int2ObjectMap) SurrogateAction(org.apache.druid.indexing.common.actions.SurrogateAction) DataSegment(org.apache.druid.timeline.DataSegment) Entry(java.util.Map.Entry) Preconditions(com.google.common.base.Preconditions) IndexIO(org.apache.druid.segment.IndexIO) DataSchema(org.apache.druid.segment.indexing.DataSchema) Collections(java.util.Collections) HashMap(java.util.HashMap) Int2ObjectOpenHashMap(it.unimi.dsi.fastutil.ints.Int2ObjectOpenHashMap) ArrayList(java.util.ArrayList) Int2ObjectMap(it.unimi.dsi.fastutil.ints.Int2ObjectMap) ArrayList(java.util.ArrayList) List(java.util.List) File(java.io.File) Interval(org.joda.time.Interval)

Aggregations

Int2ObjectOpenHashMap (it.unimi.dsi.fastutil.ints.Int2ObjectOpenHashMap)29 Int2ObjectMap (it.unimi.dsi.fastutil.ints.Int2ObjectMap)8 List (java.util.List)7 IOException (java.io.IOException)6 ArrayList (java.util.ArrayList)6 HashSet (java.util.HashSet)6 Int2IntOpenHashMap (it.unimi.dsi.fastutil.ints.Int2IntOpenHashMap)5 HashMap (java.util.HashMap)5 JsonProperty (com.fasterxml.jackson.annotation.JsonProperty)2 Preconditions (com.google.common.base.Preconditions)2 Stopwatch (com.google.common.base.Stopwatch)2 ImmutableMap (com.google.common.collect.ImmutableMap)2 Maps (com.google.common.collect.Maps)2 DBIDArrayIter (de.lmu.ifi.dbs.elki.database.ids.DBIDArrayIter)2 ModifiableDBIDs (de.lmu.ifi.dbs.elki.database.ids.ModifiableDBIDs)2 FiniteProgress (de.lmu.ifi.dbs.elki.logging.progress.FiniteProgress)2 AbortException (de.lmu.ifi.dbs.elki.utilities.exceptions.AbortException)2 ObjectIterator (it.unimi.dsi.fastutil.objects.ObjectIterator)2 LinkedHashMap (java.util.LinkedHashMap)2 LinkedHashSet (java.util.LinkedHashSet)2