use of de.topobyte.osm4j.core.model.iface.OsmNode in project osm4j-pbf by topobyte.
the class PbfWriter method serializeDense.
private Osmformat.PrimitiveGroup serializeDense(Collection<OsmNode> nodes) {
Osmformat.PrimitiveGroup.Builder builder = Osmformat.PrimitiveGroup.newBuilder();
long lastlat = 0, lastlon = 0, lastid = 0;
Osmformat.DenseNodes.Builder bi = Osmformat.DenseNodes.newBuilder();
// Does anything in this block have tags?
boolean doesBlockHaveTags = false;
for (OsmNode node : nodes) {
if (node.getNumberOfTags() != 0) {
doesBlockHaveTags = true;
break;
}
}
// Find out if any of the nodes has metadata. If none does, we can omit
// the metadata completely.
boolean hasMetadata = false;
for (OsmNode node : nodes) {
if (node.getMetadata() != null) {
hasMetadata = true;
}
}
if (writeMetadata && hasMetadata) {
Osmformat.DenseInfo.Builder bdi = Osmformat.DenseInfo.newBuilder();
serializeMetadataDense(bdi, nodes);
bi.setDenseinfo(bdi);
}
for (OsmNode node : nodes) {
long id = node.getId();
int lat = mapDegrees(node.getLatitude());
int lon = mapDegrees(node.getLongitude());
bi.addId(id - lastid);
lastid = id;
bi.addLon(lon - lastlon);
lastlon = lon;
bi.addLat(lat - lastlat);
lastlat = lat;
// Then we must include tag information.
if (doesBlockHaveTags) {
for (int k = 0; k < node.getNumberOfTags(); k++) {
OsmTag t = node.getTag(k);
bi.addKeysVals(stringTable.getIndex(t.getKey()));
bi.addKeysVals(stringTable.getIndex(t.getValue()));
}
// Add delimiter.
bi.addKeysVals(0);
}
}
builder.setDense(bi);
return builder.build();
}
use of de.topobyte.osm4j.core.model.iface.OsmNode in project osm4j-pbf by topobyte.
the class PbfWriter method serializeNonDense.
private Osmformat.PrimitiveGroup serializeNonDense(Collection<OsmNode> nodes) {
Osmformat.PrimitiveGroup.Builder builder = Osmformat.PrimitiveGroup.newBuilder();
for (OsmNode node : nodes) {
Osmformat.Node.Builder bi = Osmformat.Node.newBuilder();
bi.setId(node.getId());
bi.setLon(mapDegrees(node.getLongitude()));
bi.setLat(mapDegrees(node.getLatitude()));
for (int k = 0; k < node.getNumberOfTags(); k++) {
OsmTag t = node.getTag(k);
bi.addKeys(stringTable.getIndex(t.getKey()));
bi.addVals(stringTable.getIndex(t.getValue()));
}
if (writeMetadata && node.getMetadata() != null) {
bi.setInfo(serializeMetadata(node));
}
builder.addNodes(bi);
}
return builder.build();
}
use of de.topobyte.osm4j.core.model.iface.OsmNode in project osm4j-pbf by topobyte.
the class PrimParser method convert.
public OsmNode convert(Osmformat.Node n) {
long id = n.getId();
double lat = parseLat(n.getLat());
double lon = parseLon(n.getLon());
List<OsmTag> tags = new ArrayList<>();
for (int j = 0; j < n.getKeysCount(); j++) {
tags.add(new Tag(strings[n.getKeys(j)], strings[n.getVals(j)]));
}
OsmMetadata metadata = null;
if (fetchMetadata && n.hasInfo()) {
Osmformat.Info info = n.getInfo();
metadata = convertMetadata(info);
}
return new Node(id, lon, lat, tags, metadata);
}
use of de.topobyte.osm4j.core.model.iface.OsmNode in project osm4j-pbf by topobyte.
the class PrimParser method convert.
public List<OsmNode> convert(Osmformat.DenseNodes nodes) {
List<OsmNode> results = new ArrayList<>(nodes.getIdCount());
Osmformat.DenseInfo denseInfo = null;
boolean hasVisible = false;
if (fetchMetadata && nodes.hasDenseinfo()) {
denseInfo = nodes.getDenseinfo();
hasVisible = denseInfo.getVisibleCount() != 0;
}
long id = 0, lat = 0, lon = 0;
int version = 0, uid = 0, userSid = 0;
long timestamp = 0, changeset = 0;
// Index into the keysvals array.
int j = 0;
for (int i = 0; i < nodes.getIdCount(); i++) {
id += nodes.getId(i);
lat += nodes.getLat(i);
lon += nodes.getLon(i);
double latf = parseLat(lat), lonf = parseLon(lon);
List<OsmTag> tags = new ArrayList<>();
OsmMetadata metadata = null;
if (fetchMetadata && nodes.hasDenseinfo()) {
version = denseInfo.getVersion(i);
timestamp += denseInfo.getTimestamp(i);
uid += denseInfo.getUid(i);
userSid += denseInfo.getUserSid(i);
changeset += denseInfo.getChangeset(i);
boolean visible = true;
if (hasVisible) {
visible = denseInfo.getVisible(i);
}
metadata = new Metadata(version, timestamp * dateGranularity, uid, strings[userSid], changeset, visible);
}
// If empty, assume that nothing here has keys or vals.
if (nodes.getKeysValsCount() > 0) {
while (nodes.getKeysVals(j) != 0) {
int keyid = nodes.getKeysVals(j++);
int valid = nodes.getKeysVals(j++);
tags.add(new Tag(strings[keyid], strings[valid]));
}
// Skip over the '0' delimiter.
j++;
}
results.add(new Node(id, lonf, latf, tags, metadata));
}
return results;
}
use of de.topobyte.osm4j.core.model.iface.OsmNode in project osm4j-pbf by topobyte.
the class CopyElementwise method main.
public static void main(String[] args) throws IOException, OsmInputException {
if (args.length != 2) {
System.out.println("usage: " + CopyElementwise.class.getSimpleName() + " <input> <output>");
System.exit(1);
}
InputStream input = new FileInputStream(args[0]);
OutputStream output = new FileOutputStream(args[1]);
final PbfWriter writer = new PbfWriter(output, true);
PbfParser parser = new PbfParser(new OsmHandler() {
@Override
public void handle(OsmBounds bounds) throws IOException {
writer.write(bounds);
}
@Override
public void handle(OsmNode node) throws IOException {
writer.write(node);
}
@Override
public void handle(OsmWay way) throws IOException {
writer.write(way);
}
@Override
public void handle(OsmRelation relation) throws IOException {
writer.write(relation);
}
@Override
public void complete() throws IOException {
writer.complete();
}
}, true);
parser.parse(input);
output.close();
}
Aggregations