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);
}
}
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;
}
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;
}
});
}
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);
}
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;
}
Aggregations