use of net.osmand.osm.edit.EntityInfo in project OsmAnd-tools by osmandapp.
the class OsmBaseStoragePbf method parseOSMPbf.
public synchronized void parseOSMPbf(final InputStream stream, final IProgress progress, final boolean entityInfo) throws IOException {
BinaryParser parser = new BinaryParser() {
public void updateProgress(int count) {
progressEntity += count;
if (progress != null && progressEntity > moduleProgress && !progress.isIndeterminate()) {
try {
progressEntity = 0;
progress.remaining(stream.available());
} catch (IOException e) {
progress.startWork(-1);
}
}
}
public void registerEntity(EntityType type, Entity e, EntityInfo info) {
EntityId entityId = new EntityId(type, e.getId());
if (acceptEntityToLoad(entityId, e)) {
Entity oldEntity = entities.put(entityId, e);
if (info != null) {
OsmBaseStoragePbf.this.entityInfo.put(entityId, info);
}
if (!supressWarnings && oldEntity != null) {
// $NON-NLS-1$ //$NON-NLS-2$
throw new UnsupportedOperationException("Entity with id=" + oldEntity.getId() + " is duplicated in osm map");
}
}
}
@Override
protected void parse(HeaderBlock header) {
}
// $NON-NLS-1$
private DateFormat format = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss'Z'");
@Override
protected void parseDense(DenseNodes n) {
EntityInfo info = null;
long changeset = 0;
long timestamp = 0;
int uid = 0;
int user = 0;
long id = 0;
long lat = 0;
long lon = 0;
int keyInd = 0;
boolean tagsEmpty = n.getKeysValsCount() == 0;
for (int i = 0; i < n.getIdCount(); i++) {
id += n.getId(i);
lat += n.getLat(i);
lon += n.getLon(i);
Node node = new Node(parseLat(lat), parseLon(lon), id);
if (entityInfo && n.getDenseinfo() != null) {
info = new EntityInfo();
changeset += n.getDenseinfo().getChangeset(i);
timestamp += n.getDenseinfo().getTimestamp(i);
uid += n.getDenseinfo().getUid(i);
user += n.getDenseinfo().getUserSid(i);
// $NON-NLS-1$
info.setChangeset(String.valueOf(changeset));
info.setTimestamp(format.format(new Date(date_granularity * (timestamp))));
info.setUser(getStringById(user));
// $NON-NLS-1$
info.setUid(String.valueOf(uid));
// $NON-NLS-1$
info.setVersion(String.valueOf(n.getDenseinfo().getVersion(i)));
// $NON-NLS-1$
info.setVisible("true");
}
if (!tagsEmpty) {
while (n.getKeysVals(keyInd) != 0) {
String key = getStringById(n.getKeysVals(keyInd));
String val = getStringById(n.getKeysVals(keyInd + 1));
node.putTag(key, val);
keyInd += 2;
}
keyInd++;
}
registerEntity(EntityType.NODE, node, info);
}
updateProgress(n.getIdCount());
}
protected EntityInfo parseEntityInfo(Info i) {
EntityInfo info = new EntityInfo();
// $NON-NLS-1$
info.setChangeset(String.valueOf(i.getChangeset()));
info.setTimestamp(format.format(getDate(i)));
info.setUser(getStringById(i.getUserSid()));
// $NON-NLS-1$
info.setUid(String.valueOf(i.getUid()));
// $NON-NLS-1$
info.setVersion(String.valueOf(i.getVersion()));
// $NON-NLS-1$
info.setVisible("true");
return info;
}
@Override
protected void parseNodes(List<crosby.binary.Osmformat.Node> n) {
EntityInfo info = null;
int nsize = n.size();
for (int i = 0; i < nsize; i++) {
crosby.binary.Osmformat.Node nod = n.get(i);
Node e = new Node(parseLat(nod.getLat()), parseLon(nod.getLon()), nod.getId());
for (int j = 0; j < nod.getKeysCount(); j++) {
String key = getStringById(nod.getKeys(j));
String val = getStringById(nod.getVals(j));
e.putTag(key, val);
}
if (entityInfo) {
info = parseEntityInfo(nod.getInfo());
}
registerEntity(EntityType.NODE, e, info);
}
updateProgress(nsize);
}
@Override
protected void parseRelations(List<crosby.binary.Osmformat.Relation> r) {
EntityInfo info = null;
int rsize = r.size();
for (int i = 0; i < rsize; i++) {
crosby.binary.Osmformat.Relation rel = r.get(i);
Relation e = new Relation(rel.getId());
long id = 0;
for (int j = 0; j < rel.getMemidsCount(); j++) {
id += rel.getMemids(j);
String role = getStringById(rel.getRolesSid(j));
MemberType t = rel.getTypes(j);
EntityType ts = EntityType.NODE;
switch(t) {
case NODE:
ts = EntityType.NODE;
break;
case WAY:
ts = EntityType.WAY;
break;
case RELATION:
ts = EntityType.RELATION;
break;
}
e.addMember(id, ts, role);
}
for (int j = 0; j < rel.getKeysCount(); j++) {
String key = getStringById(rel.getKeys(j));
String val = getStringById(rel.getVals(j));
e.putTag(key, val);
}
if (entityInfo) {
info = parseEntityInfo(rel.getInfo());
}
registerEntity(EntityType.RELATION, e, info);
}
updateProgress(rsize);
}
@Override
protected void parseWays(List<crosby.binary.Osmformat.Way> w) {
EntityInfo info = null;
int wsize = w.size();
for (int i = 0; i < wsize; i++) {
crosby.binary.Osmformat.Way way = w.get(i);
Way e = new Way(way.getId());
long id = 0;
for (int j = 0; j < way.getRefsCount(); j++) {
id += way.getRefs(j);
e.addNode(id);
}
for (int j = 0; j < way.getKeysCount(); j++) {
String key = getStringById(way.getKeys(j));
String val = getStringById(way.getVals(j));
e.putTag(key, val);
}
if (entityInfo) {
info = parseEntityInfo(way.getInfo());
}
registerEntity(EntityType.WAY, e, info);
}
updateProgress(wsize);
}
@Override
public void complete() {
}
};
this.progressEntity = 0;
this.entities.clear();
this.entityInfo.clear();
if (progress != null) {
progress.startWork(stream.available());
}
BlockInputStream bis = new BlockInputStream(stream, parser);
bis.process();
if (progress != null) {
progress.finishTask();
}
completeReading();
}
use of net.osmand.osm.edit.EntityInfo 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;
}
use of net.osmand.osm.edit.EntityInfo 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.EntityInfo in project OsmAnd-tools by osmandapp.
the class AHSupermarketResolver method updateOSMFile.
// this file could be retrieved using xapi
// http://xapi.openstreetmap.org/api/0.6/*[shop=supermarket][bbox=2.5,50,7.8,53.5]
public void updateOSMFile(String pathToOsmFile, String pathToModifiedFile, boolean show) throws IOException, XMLStreamException, JSONException, XmlPullParserException {
OsmBaseStorage storage = new OsmBaseStorage();
final Map<String, EntityId> winkelNumbers = new LinkedHashMap<String, EntityId>();
storage.getFilters().add(new IOsmStorageFilter() {
@Override
public boolean acceptEntityToLoad(OsmBaseStorage storage, EntityId entityId, Entity entity) {
if (entity.getTag("winkelnummer") != null && entity.getTag("name").contains("eijn")) {
winkelNumbers.put(entity.getTag("winkelnummer"), entityId);
return true;
}
// register all nodes in order to operate with ways
return true;
}
});
storage.parseOSM(new FileInputStream(pathToOsmFile), new ConsoleProgressImplementation(2), null, true);
Map<String, Map<String, Object>> supermarkets = getSupermarkets();
DataTileManager<Entity> deleted = new DataTileManager<Entity>();
for (String s : winkelNumbers.keySet()) {
if (!supermarkets.containsKey(s)) {
System.err.println("Shop " + s + " id=" + winkelNumbers.get(s) + " doesn't present on the site.");
EntityId e = winkelNumbers.get(s);
Entity en = storage.getRegisteredEntities().get(e);
deleted.registerObject(en.getLatLon().getLatitude(), en.getLatLon().getLongitude(), en);
}
}
DataTileManager<Entity> notCorrelated = new DataTileManager<Entity>();
DataTileManager<Entity> notShown = new DataTileManager<Entity>();
for (String s : supermarkets.keySet()) {
Map<String, Object> props = supermarkets.get(s);
if (winkelNumbers.get(s) != null) {
EntityId id = winkelNumbers.get(s);
Entity e = storage.getRegisteredEntities().get(id);
EntityInfo info = storage.getRegisteredEntityInfo().get(id);
Map<String, String> newTags = new LinkedHashMap<String, String>();
String p = String.valueOf(props.get("format"));
// IMPORTANT : comment what information should be updated or check
String name = "Albert Heijn";
if (!p.equals("AH")) {
name += " " + p;
}
newTags.put("name", name);
newTags.put("phone", String.valueOf(props.get("phone")));
newTags.put("addr:city", String.valueOf(props.get("city")));
newTags.put("addr:street", String.valueOf(props.get("street")));
newTags.put("addr:housenumber", String.valueOf(props.get("housenr")));
newTags.put("addr:postcode", String.valueOf(props.get("zip")));
JSONArray o = (JSONArray) props.get("hours");
OpeningHoursParser.OpeningHours rules = new OpeningHoursParser.OpeningHours();
BasicOpeningHourRule prev = null;
for (int i = 0; i < 7; i++) {
JSONObject obj = o.getJSONObject(i);
if (!obj.isNull("C") && obj.getBoolean("C")) {
} else {
String opened = String.valueOf(obj.get("F"));
String closed = String.valueOf(obj.get("U"));
int start = Integer.parseInt(opened.substring(0, 2)) * 60 + Integer.parseInt(opened.substring(2));
int end = Integer.parseInt(closed.substring(0, 2)) * 60 + Integer.parseInt(closed.substring(2));
if (prev != null && prev.getStartTime() == start && prev.getEndTime() == end) {
prev.getDays()[i] = true;
} else {
BasicOpeningHourRule rule = new OpeningHoursParser.BasicOpeningHourRule();
rule.getDays()[i] = true;
rule.addTimeRange(start, end);
prev = rule;
rules.addRule(rule);
}
}
}
newTags.put("opening_hours", rules.toString());
// Check distance to info
LatLon real = new LatLon((Double) props.get("lat"), (Double) props.get("lng"));
double dist = MapUtils.getDistance(e.getLatLon(), real);
if (dist > 150) {
// TODO move shop ?
System.err.println("Winkel number = " + s + " is too far from site info - " + dist + " m !!! " + real);
if (dist > 300) {
notCorrelated.registerObject(real.getLatitude(), real.getLongitude(), e);
}
}
boolean changed = false;
for (String k : newTags.keySet()) {
String val = newTags.get(k);
if (!Algorithms.objectEquals(val, e.getTag(k))) {
e.putTag(k, val);
changed = true;
}
}
if (changed) {
info.setAction("modify");
}
} else {
// TODO?
LatLon real = new LatLon((Double) props.get("lat"), (Double) props.get("lng"));
System.err.println("Winkel number = " + s + " is not found in database !!! " + real);
Node n = new Node(real.getLatitude(), real.getLongitude(), -1);
n.putTag("winkelnummer", "REG : " + s);
notShown.registerObject(real.getLatitude(), real.getLongitude(), n);
}
}
OsmStorageWriter writer = new OsmStorageWriter();
writer.saveStorage(new FileOutputStream(pathToModifiedFile), storage, null, true);
if (show) {
try {
UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
} catch (Exception e) {
e.printStackTrace();
}
final MapPanel panel = new MapPanel(DataExtractionSettings.getSettings().getTilesDirectory());
panel.setFocusable(true);
MapPointsLayer toAdd = panel.getLayer(MapPointsLayer.class);
toAdd.setPoints(notShown);
toAdd.setPointSize(5);
toAdd.setTagToShow("winkelnummer");
MapPointsLayer red = new MapPointsLayer();
red.setPoints(deleted);
red.setColor(Color.red);
red.setPointSize(5);
panel.addLayer(red);
MapPointsLayer blue = new MapPointsLayer();
blue.setPoints(notCorrelated);
blue.setColor(Color.blue);
blue.setPointSize(4);
panel.addLayer(blue);
JFrame frame = new JFrame("Map view");
frame.addWindowListener(new WindowAdapter() {
@Override
public void windowClosing(WindowEvent e) {
DataExtractionSettings settings = DataExtractionSettings.getSettings();
settings.saveDefaultLocation(panel.getLatitude(), panel.getLongitude());
settings.saveDefaultZoom(panel.getZoom());
System.exit(0);
}
});
Container content = frame.getContentPane();
content.add(panel, BorderLayout.CENTER);
JMenuBar bar = new JMenuBar();
bar.add(MapPanel.getMenuToChooseSource(panel));
frame.setJMenuBar(bar);
frame.setSize(512, 512);
frame.setVisible(true);
}
}
use of net.osmand.osm.edit.EntityInfo 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