use of net.osmand.osm.edit.Relation in project OsmAnd-tools by osmandapp.
the class IndexAddressCreator method indexBoundariesRelation.
public void indexBoundariesRelation(Entity e, OsmDbAccessorContext ctx) throws SQLException {
Boundary boundary = extractBoundary(e, ctx);
// Bucharest has admin level 4
boolean boundaryValid = boundary != null && (!boundary.hasAdminLevel() || boundary.getAdminLevel() >= 4) && boundary.getCenterPoint() != null && !Algorithms.isEmpty(boundary.getName());
if (boundaryValid) {
LatLon boundaryCenter = boundary.getCenterPoint();
List<City> citiesToSearch = new ArrayList<City>();
citiesToSearch.addAll(cityManager.getClosestObjects(boundaryCenter.getLatitude(), boundaryCenter.getLongitude(), 3));
citiesToSearch.addAll(cityVillageManager.getClosestObjects(boundaryCenter.getLatitude(), boundaryCenter.getLongitude(), 3));
City cityFound = null;
String boundaryName = boundary.getName().toLowerCase();
String altBoundaryName = Algorithms.isEmpty(boundary.getAltName()) ? "" : boundary.getAltName().toLowerCase();
if (boundary.hasAdminCenterId()) {
for (City c : citiesToSearch) {
if (c.getId() == boundary.getAdminCenterId()) {
cityFound = c;
break;
}
}
}
if (cityFound == null) {
for (City c : citiesToSearch) {
if ((boundaryName.equalsIgnoreCase(c.getName()) || altBoundaryName.equalsIgnoreCase(c.getName())) && boundary.containsPoint(c.getLocation())) {
cityFound = c;
break;
}
}
}
// False case : London Borough of Richmond upon Thames (bigger) -> Richmond!
if (cityFound == null) {
for (City c : citiesToSearch) {
String lower = c.getName().toLowerCase();
if (nameContains(boundaryName, lower) || nameContains(altBoundaryName, lower)) {
if (boundary.containsPoint(c.getLocation())) {
cityFound = c;
break;
}
}
}
}
// It could be wrong if the boundary doesn't match center point
if (cityFound == null && /*&& !boundary.hasAdminLevel() */
(boundary.getCityType() == CityType.TOWN || // boundary.getCityType() == CityType.CITY ||
boundary.getCityType() == CityType.HAMLET || boundary.getCityType() == CityType.SUBURB || boundary.getCityType() == CityType.VILLAGE)) {
if (e instanceof Relation) {
ctx.loadEntityRelation((Relation) e);
}
cityFound = createMissingCity(e, boundary.getCityType());
boundary.setAdminCenterId(cityFound.getId());
}
if (cityFound != null) {
putCityBoundary(boundary, cityFound);
} else {
logBoundaryChanged(boundary, null, 0, 0);
notAssignedBoundaries.add(boundary);
}
attachAllCitiesToBoundary(boundary);
} else if (boundary != null) {
if (logMapDataWarn != null) {
logMapDataWarn.warn("Not using boundary: " + boundary + " " + boundary.getBoundaryId());
} else {
log.info("Not using boundary: " + boundary + " " + boundary.getBoundaryId());
}
}
}
use of net.osmand.osm.edit.Relation in project OsmAnd-tools by osmandapp.
the class IndexAddressCreator method iterateMainEntity.
public void iterateMainEntity(Entity e, OsmDbAccessorContext ctx) throws SQLException {
Map<String, String> tags = e.getTags();
// index not only buildings but also nodes that belongs to addr:interpolation ways
// currently not supported because nodes are indexed first with buildings
String interpolation = e.getTag(OSMTagKey.ADDR_INTERPOLATION);
if (e instanceof Way && interpolation != null) {
BuildingInterpolation type = null;
int interpolationInterval = 0;
try {
type = BuildingInterpolation.valueOf(interpolation.toUpperCase());
} catch (RuntimeException ex) {
try {
interpolationInterval = Integer.parseInt(interpolation);
} catch (NumberFormatException ex2) {
}
}
if (type != null || interpolationInterval > 0) {
List<Node> nodesWithHno = new ArrayList<Node>();
for (Node n : ((Way) e).getNodes()) {
if (n.getTag(OSMTagKey.ADDR_HOUSE_NUMBER) != null) {
String strt = n.getTag(OSMTagKey.ADDR_STREET);
if (strt == null) {
strt = n.getTag(OSMTagKey.ADDR_PLACE);
}
if (strt != null) {
nodesWithHno.add(n);
}
}
}
if (nodesWithHno.size() > 1) {
for (int i = 1; i < nodesWithHno.size(); i++) {
Node first = nodesWithHno.get(i - 1);
Node second = nodesWithHno.get(i);
boolean exist = streetDAO.findBuilding(first);
if (exist) {
streetDAO.removeBuilding(first);
}
LatLon l = e.getLatLon();
String strt = first.getTag(OSMTagKey.ADDR_STREET);
if (strt == null) {
strt = first.getTag(OSMTagKey.ADDR_PLACE);
}
Set<Long> idsOfStreet = getStreetInCity(first.getIsInNames(), strt, null, l);
if (!idsOfStreet.isEmpty()) {
Building building = EntityParser.parseBuilding(first);
building.setInterpolationInterval(interpolationInterval);
building.setInterpolationType(type);
building.setName(first.getTag(OSMTagKey.ADDR_HOUSE_NUMBER));
building.setName2(second.getTag(OSMTagKey.ADDR_HOUSE_NUMBER));
building.setLatLon2(second.getLatLon());
streetDAO.writeBuilding(idsOfStreet, building);
}
}
}
}
}
String houseName = e.getTag(OSMTagKey.ADDR_HOUSE_NAME);
String houseNumber = e.getTag(OSMTagKey.ADDR_HOUSE_NUMBER);
String street = null;
if (houseNumber != null) {
street = e.getTag(OSMTagKey.ADDR_STREET);
if (street == null) {
street = e.getTag(OSMTagKey.ADDR_PLACE);
}
}
String street2 = e.getTag(OSMTagKey.ADDR_STREET2);
if ((houseName != null || houseNumber != null)) {
if (e instanceof Relation) {
ctx.loadEntityRelation((Relation) e);
Collection<Entity> outs = ((Relation) e).getMemberEntities("outer");
if (!outs.isEmpty()) {
e = outs.iterator().next();
}
}
// skip relations
boolean exist = e instanceof Relation || streetDAO.findBuilding(e);
if (!exist) {
LatLon l = e.getLatLon();
Set<Long> idsOfStreet = getStreetInCity(e.getIsInNames(), street, null, l);
if (!idsOfStreet.isEmpty()) {
Building building = EntityParser.parseBuilding(e);
String hname = null;
String second = null;
if (DataExtractionSettings.getSettings().isHousenumberPrefered()) {
hname = houseNumber;
second = houseName;
} else {
hname = houseName;
second = houseNumber;
}
if (hname == null) {
hname = second;
second = null;
}
String additionalHname = "";
if (DataExtractionSettings.getSettings().isAdditionalInfo() && second != null)
additionalHname = " - [" + second + "]";
int i = hname.indexOf('-');
if (i != -1 && interpolation != null) {
building.setInterpolationInterval(1);
try {
building.setInterpolationType(BuildingInterpolation.valueOf(interpolation.toUpperCase()));
} catch (RuntimeException ex) {
try {
building.setInterpolationInterval(Integer.parseInt(interpolation));
} catch (NumberFormatException ex2) {
}
}
building.setName(hname.substring(0, i));
building.setName2(hname.substring(i + 1));
} else if ((street2 != null) && !street2.isEmpty()) {
int secondNumber = hname.indexOf('/');
if (secondNumber == -1 || !(secondNumber < hname.length() - 1)) {
building.setName(hname + additionalHname);
} else {
building.setName(hname.substring(0, secondNumber) + additionalHname);
Building building2 = EntityParser.parseBuilding(e);
building2.setName(hname.substring(secondNumber + 1) + additionalHname);
Set<Long> ids2OfStreet = getStreetInCity(e.getIsInNames(), street2, null, l);
// remove duplicated entries!
ids2OfStreet.removeAll(idsOfStreet);
if (!ids2OfStreet.isEmpty()) {
streetDAO.writeBuilding(ids2OfStreet, building2);
} else {
building.setName2(building2.getName() + additionalHname);
}
}
} else {
building.setName(hname + additionalHname);
}
streetDAO.writeBuilding(idsOfStreet, building);
}
}
} else if (e instanceof Way && /* && OSMSettings.wayForCar(e.getTag(OSMTagKey.HIGHWAY)) */
e.getTag(OSMTagKey.HIGHWAY) != null && e.getTag(OSMTagKey.NAME) != null && isStreetTag(e.getTag(OSMTagKey.HIGHWAY))) {
// suppose that streets with names are ways for car
// Ignore all ways that have house numbers and highway type
// if we saved address ways we could checked that we registered before
boolean exist = streetDAO.findStreetNode(e);
// check that street way is not registered already
if (!exist) {
LatLon l = e.getLatLon();
Set<Long> idsOfStreet = getStreetInCity(e.getIsInNames(), e.getTag(OSMTagKey.NAME), getOtherNames(e), l);
if (!idsOfStreet.isEmpty()) {
streetDAO.writeStreetWayNodes(idsOfStreet, (Way) e);
}
}
}
if (e.getTag(OSMTagKey.POSTAL_CODE) != null) {
if ("postal_code".equals(e.getTag(OSMTagKey.BOUNDARY))) {
Boundary boundary = extractBoundary(e, ctx);
if (boundary != null) {
postcodeBoundaries.put(e, boundary);
}
} else if (e instanceof Relation) {
ctx.loadEntityRelation((Relation) e);
postalCodeRelations.add((Relation) e);
}
}
}
use of net.osmand.osm.edit.Relation 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);
}
use of net.osmand.osm.edit.Relation in project OsmAnd-tools by osmandapp.
the class AugmentedDiffsInspector method prepareRegions.
private void prepareRegions(Context ctx, Map<EntityId, Entity> ids, Map<String, Set<EntityId>> regionsMap, OsmandRegions osmandRegions) throws IOException {
Map<EntityId, Set<String>> mp = new HashMap<Entity.EntityId, Set<String>>();
for (Entity e : ids.values()) {
if (e instanceof Node) {
int y = MapUtils.get31TileNumberY(((Node) e).getLatitude());
int x = MapUtils.get31TileNumberX(((Node) e).getLongitude());
EntityId id = EntityId.valueOf(e);
TreeSet<String> lst = new TreeSet<String>();
mp.put(id, lst);
if (osmandRegions == null) {
addEntityToRegion(regionsMap, id, lst, DEFAULT_REGION);
} else {
List<BinaryMapDataObject> l = osmandRegions.query(x, y);
for (BinaryMapDataObject b : l) {
if (osmandRegions.contain(b, x, y)) {
String dw = osmandRegions.getDownloadName(b);
if (!Algorithms.isEmpty(dw) && osmandRegions.isDownloadOfType(b, OsmandRegions.MAP_TYPE)) {
addEntityToRegion(regionsMap, id, lst, dw);
}
}
}
}
}
}
// 2. add ways and complete ways with missing nodes
for (Entity e : ids.values()) {
if (e instanceof Way) {
Way w = (Way) e;
EntityId wid = EntityId.valueOf(w);
TreeSet<String> lst = new TreeSet<String>();
mp.put(wid, lst);
for (EntityId it : w.getEntityIds()) {
Set<String> countries = mp.get(it);
for (String cnt : countries) {
regionsMap.get(cnt).add(wid);
}
lst.addAll(countries);
}
// complete ways with missing nodes
for (EntityId it : w.getEntityIds()) {
mp.get(it).addAll(lst);
for (String s : lst) {
regionsMap.get(s).add(it);
}
}
}
}
// 3. add relations (not complete with ways or nodes)
for (Entity e : ids.values()) {
if (e instanceof Relation) {
Relation r = (Relation) e;
EntityId rid = EntityId.valueOf(r);
TreeSet<String> lst = new TreeSet<String>();
mp.put(rid, lst);
for (RelationMember it : r.getMembers()) {
Set<String> countries = mp.get(it.getEntityId());
for (String cnt : countries) {
regionsMap.get(cnt).add(rid);
}
lst.addAll(countries);
}
}
}
}
use of net.osmand.osm.edit.Relation in project Osmand by osmandapp.
the class OsmBaseStorage method startElement.
public void startElement(XmlPullParser parser, String name) {
if (!parseStarted) {
initRootElement(parser, name);
}
if (ELEM_MODIFY.equals(name)) {
currentModify = Entity.MODIFY_MODIFIED;
} else if (ELEM_CREATE.equals(name)) {
currentModify = Entity.MODIFY_CREATED;
} else if (ELEM_DELETE.equals(name)) {
currentModify = Entity.MODIFY_DELETED;
} else if (currentParsedEntity == null) {
progressEntity++;
if (progress != null && ((progressEntity % moduleProgress) == 0) && !progress.isIndeterminate() && streamForProgress != null) {
try {
progress.remaining(streamForProgress.available());
} catch (IOException e) {
progress.startWork(-1);
}
}
if (ELEM_NODE.equals(name)) {
currentParsedEntity = new Node(parseDouble(parser, ATTR_LAT, 0), parseDouble(parser, ATTR_LON, 0), parseId(parser, ATTR_ID, -1));
currentParsedEntity.setVersion(parseVersion(parser));
} else if (ELEM_WAY.equals(name)) {
currentParsedEntity = new Way(parseId(parser, ATTR_ID, -1));
currentParsedEntity.setVersion(parseVersion(parser));
} else if (ELEM_RELATION.equals(name)) {
currentParsedEntity = new Relation(parseId(parser, ATTR_ID, -1));
} else {
// this situation could be logged as unhandled
}
if (currentParsedEntity != null) {
currentParsedEntity.setModify(currentModify);
if (parseEntityInfo) {
currentParsedEntityInfo = new EntityInfo();
currentParsedEntityInfo.setChangeset(parser.getAttributeValue("", ATTR_CHANGESET));
currentParsedEntityInfo.setTimestamp(parser.getAttributeValue("", ATTR_TIMESTAMP));
currentParsedEntityInfo.setUser(parser.getAttributeValue("", ATTR_USER));
currentParsedEntityInfo.setVersion(parser.getAttributeValue("", ATTR_VERSION));
currentParsedEntityInfo.setVisible(parser.getAttributeValue("", ATTR_VISIBLE));
currentParsedEntityInfo.setUid(parser.getAttributeValue("", ATTR_UID));
}
}
} else {
if (ELEM_TAG.equals(name)) {
String key = parser.getAttributeValue("", ATTR_K);
if (key != null) {
if (convertTagsToLC) {
currentParsedEntity.putTag(key, parser.getAttributeValue("", ATTR_V));
} else {
currentParsedEntity.putTagNoLC(key, parser.getAttributeValue("", ATTR_V));
}
}
} else if (ELEM_ND.equals(name)) {
Long id = parseId(parser, ATTR_REF, -1);
if (id != -1 && currentParsedEntity instanceof Way) {
((Way) currentParsedEntity).addNode(id);
}
} else if (ELEM_MEMBER.equals(name)) {
try {
Long id = parseId(parser, ATTR_REF, -1);
if (id != -1 && currentParsedEntity instanceof Relation) {
EntityType type = EntityType.valueOf(parser.getAttributeValue("", ATTR_TYPE).toUpperCase());
((Relation) currentParsedEntity).addMember(id, type, parser.getAttributeValue("", ATTR_ROLE));
}
} catch (Exception e) {
e.printStackTrace();
}
} else {
// this situation could be logged as unhandled
}
}
}
Aggregations