use of net.osmand.osm.edit.Entity.EntityId in project OsmAnd-tools by osmandapp.
the class IndexCreator method extractOsmToNodesDB.
private OsmDbCreator extractOsmToNodesDB(OsmDbAccessor accessor, File readFile, IProgress progress, IOsmStorageFilter addFilter, int additionId, int shiftId, boolean ovewriteIds, boolean generateNewIds, boolean createTables, OsmDbCreator previous) throws IOException, SQLException, XmlPullParserException {
boolean pbfFile = false;
InputStream stream = new BufferedInputStream(new FileInputStream(readFile), 8192 * 4);
InputStream streamFile = stream;
long st = System.currentTimeMillis();
if (readFile.getName().endsWith(".bz2")) {
// $NON-NLS-1$
if (stream.read() != 'B' || stream.read() != 'Z') {
// throw new RuntimeException("The source stream must start with the characters BZ if it is to be read as a BZip2 stream."); //$NON-NLS-1$
} else {
stream = new CBZip2InputStream(stream);
}
} else if (readFile.getName().endsWith(".gz")) {
// $NON-NLS-1$
stream = new GZIPInputStream(stream);
} else if (readFile.getName().endsWith(".pbf")) {
// $NON-NLS-1$
pbfFile = true;
}
OsmBaseStorage storage = pbfFile ? new OsmBaseStoragePbf() : new OsmBaseStorage();
storage.setSupressWarnings(DataExtractionSettings.getSettings().isSupressWarningsForDuplicatedId());
if (addFilter != null) {
storage.getFilters().add(addFilter);
}
storage.getFilters().add(new IOsmStorageFilter() {
@Override
public boolean acceptEntityToLoad(OsmBaseStorage storage, EntityId entityId, Entity entity) {
if (indexAddressCreator != null) {
indexAddressCreator.registerCityIfNeeded(entity);
}
// accept to allow db creator parse it
return true;
}
});
// 1. Loading osm file
OsmDbCreator dbCreator = new OsmDbCreator(additionId, shiftId, ovewriteIds, generateNewIds);
if (previous != null) {
dbCreator.setNodeIds(previous.getNodeIds());
dbCreator.setWayIds(previous.getWayIds());
dbCreator.setRelationIds(previous.getRelationIds());
}
dbCreator.setBackwardCompatibleIds(backwardCompatibleIds);
try {
// $NON-NLS-1$
setGeneralProgress(progress, "[15 / 100]");
// $NON-NLS-1$
progress.startTask(Messages.getString("IndexCreator.LOADING_FILE") + readFile.getAbsolutePath(), -1);
// 1 init database to store temporary data
dbCreator.initDatabase(osmDBdialect, accessor.getDbConn(), createTables);
storage.getFilters().add(dbCreator);
if (pbfFile) {
((OsmBaseStoragePbf) storage).parseOSMPbf(stream, progress, false);
} else {
storage.parseOSM(stream, progress, streamFile, false);
}
dbCreator.finishLoading();
osmDBdialect.commitDatabase(accessor.getDbConn());
if (log.isInfoEnabled()) {
// $NON-NLS-1$
log.info("File parsed : " + (System.currentTimeMillis() - st));
}
progress.finishTask();
return dbCreator;
} finally {
if (log.isInfoEnabled()) {
// $NON-NLS-1$
log.info("File indexed : " + (System.currentTimeMillis() - st));
}
}
}
use of net.osmand.osm.edit.Entity.EntityId in project OsmAnd-tools by osmandapp.
the class FixBasemapRoads method process.
private void process(File read, File write, List<File> relationFiles) throws IOException, XMLStreamException, XmlPullParserException, SQLException {
MapRenderingTypesEncoder renderingTypes = new MapRenderingTypesEncoder("basemap");
OsmandRegions or = prepareRegions();
TagsTransformer transformer = new TagsTransformer();
for (File relFile : relationFiles) {
LOG.info("Parse relations file " + relFile.getName());
OsmBaseStorage storage = parseOsmFile(relFile);
int total = 0;
for (EntityId e : storage.getRegisteredEntities().keySet()) {
if (e.getType() == EntityType.RELATION) {
total++;
if (total % 1000 == 0) {
LOG.info("Processed " + total + " relations");
}
Relation es = (Relation) storage.getRegisteredEntities().get(e);
transformer.handleRelationPropogatedTags(es, renderingTypes, null, EntityConvertApplyType.MAP);
}
}
}
LOG.info("Parse main file " + read.getName());
OsmBaseStorage storage = parseOsmFile(read);
Map<EntityId, Entity> entities = new HashMap<EntityId, Entity>(storage.getRegisteredEntities());
int total = 0;
for (EntityId e : entities.keySet()) {
if (e.getType() == EntityType.WAY) {
Way es = (Way) storage.getRegisteredEntities().get(e);
total++;
if (total % 1000 == 0) {
LOG.info("Processed " + total + " ways");
}
addRegionTag(or, es);
transformer.addPropogatedTags(es);
Map<String, String> ntags = renderingTypes.transformTags(es.getModifiableTags(), EntityType.WAY, EntityConvertApplyType.MAP);
if (es.getModifiableTags() != ntags) {
es.getModifiableTags().putAll(ntags);
}
processWay(es);
}
}
List<EntityId> toWrite = new ArrayList<EntityId>();
processRegion(toWrite);
OsmStorageWriter writer = new OsmStorageWriter();
LOG.info("Writing file... ");
writer.saveStorage(new FileOutputStream(write), storage, toWrite, true);
LOG.info("DONE");
}
use of net.osmand.osm.edit.Entity.EntityId in project OsmAnd-tools by osmandapp.
the class FixLinkedCoastline method process.
private static void process(File read, File write) throws IOException, XMLStreamException, XmlPullParserException {
OsmBaseStorage storage = new OsmBaseStorage();
storage.parseOSM(new FileInputStream(read), new ConsoleProgressImplementation());
Map<EntityId, Entity> entities = new HashMap<EntityId, Entity>(storage.getRegisteredEntities());
List<EntityId> toWrite = new ArrayList<EntityId>();
for (EntityId e : entities.keySet()) {
if (e.getType() == EntityType.WAY) {
Entity oldWay = storage.getRegisteredEntities().remove(e);
List<Way> result = processWay((Way) oldWay);
alignAndAddtoStorage(storage, toWrite, result);
}
}
System.out.println("ERROR Ways : ");
int errors = 0;
for (List<Way> w : endWays.values()) {
Way way = w.get(0);
Way lway = w.get(w.size() - 1);
LatLon first = way.getNodes().get(0).getLatLon();
LatLon last = lway.getNodes().get(lway.getNodes().size() - 1).getLatLon();
double dist = MapUtils.getDistance(first, last);
if (dist < 500000) {
alignAndAddtoStorage(storage, toWrite, w);
} else {
errors++;
String val = "First " + first + "Last " + last + " id " + way.getId() + " dist " + MapUtils.getDistance(first, last) + " m";
System.out.println("Ways in chain - " + w.size() + " - " + val);
}
}
System.out.println("Fixed errors : " + ERRORS + ", errors not fixed : " + errors);
OsmStorageWriter writer = new OsmStorageWriter();
/*Set<Long> ls = new HashSet<Long>();
for(EntityId e : toWrite) {
if(!ls.add(e.getId())) {
System.out.println("!!" + e.getId());
}
}*/
writer.saveStorage(new FileOutputStream(write), storage, toWrite, true);
}
use of net.osmand.osm.edit.Entity.EntityId in project OsmAnd-tools by osmandapp.
the class TagsTransformer method addPropogatedTags.
public void addPropogatedTags(Entity e) {
EntityId eid = EntityId.valueOf(e);
Map<String, String> proptags = propogatedTags.get(eid);
if (proptags != null) {
Iterator<Entry<String, String>> iterator = proptags.entrySet().iterator();
while (iterator.hasNext()) {
Entry<String, String> ts = iterator.next();
if (e.getTag(ts.getKey()) == null) {
String vl = ts.getValue();
if (vl != null) {
vl = vl.replaceAll(SPLIT_VALUE, ", ");
}
e.putTag(ts.getKey(), vl);
}
}
}
}
use of net.osmand.osm.edit.Entity.EntityId 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);
}
Aggregations