[osmpbf] 01/22: Java file read demonstration, to help people getting started.
Bas Couwenberg
sebastic at xs4all.nl
Sun Mar 16 10:08:41 UTC 2014
This is an automated email from the git hooks/post-receive script.
sebastic-guest pushed a commit to tag v1.3.1
in repository osmpbf.
commit fc795d3313919b1ce5fe9c4ff129db5d70afd709
Author: michael <michael at 192.168.1.3>
Date: Fri Oct 19 23:00:21 2012 +0100
Java file read demonstration, to help people getting started.
---
README | 2 +
pom.xml | 2 +
resources/sample.pbf | Bin 0 -> 9653 bytes
src.java/crosby/binary/test/ReadFileExample.java | 86 +++++++++++++++++++++++
4 files changed, 90 insertions(+)
diff --git a/README b/README
index 5006cda..2e8517b 100644
--- a/README
+++ b/README
@@ -6,6 +6,8 @@ See http://wiki.openstreetmap.org/wiki/PBF_Format .
There is a Java and a C version of the PBF library code here.
+For a Java usage example, see src.java\crosby\binary\test\ReadFileExample.java
+
C Version
=========
diff --git a/pom.xml b/pom.xml
index d1e3024..f5172dc 100644
--- a/pom.xml
+++ b/pom.xml
@@ -26,6 +26,8 @@
<sourceDirectory>${basedir}/src.java</sourceDirectory>
<outputDirectory>${basedir}/build</outputDirectory>
+ <resources><resource><directory>${basedir}/resources</directory></resource></resources>
+
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
diff --git a/resources/sample.pbf b/resources/sample.pbf
new file mode 100644
index 0000000..8a22edf
Binary files /dev/null and b/resources/sample.pbf differ
diff --git a/src.java/crosby/binary/test/ReadFileExample.java b/src.java/crosby/binary/test/ReadFileExample.java
new file mode 100644
index 0000000..082d406
--- /dev/null
+++ b/src.java/crosby/binary/test/ReadFileExample.java
@@ -0,0 +1,86 @@
+package crosby.binary.test;
+
+import crosby.binary.*;
+import crosby.binary.Osmformat.*;
+import crosby.binary.file.*;
+import java.io.*;
+import java.util.List;
+
+/**
+ * Demonstrates how to read a file. Reads sample.pbf from the resources folder
+ * and prints details about it to the standard output.
+ *
+ * @author Michael Tandy
+ */
+public class ReadFileExample {
+
+ public static void main(String[] args) throws Exception {
+ InputStream input = ReadFileExample.class.getResourceAsStream("/sample.pbf");
+ BlockReaderAdapter brad = new TestBinaryParser();
+ new BlockInputStream(input, brad).process();
+ }
+
+ private static class TestBinaryParser extends BinaryParser {
+
+ @Override
+ protected void parseRelations(List<Relation> rels) {
+ if (!rels.isEmpty())
+ System.out.println("Got some relations to parse.");
+ Relation r = null;
+ }
+
+ @Override
+ protected void parseDense(DenseNodes nodes) {
+ long lastId=0;
+ long lastLat=0;
+ long lastLon=0;
+
+ for (int i=0 ; i<nodes.getIdCount() ; i++) {
+ lastId += nodes.getId(i);
+ lastLat += nodes.getLat(i);
+ lastLon += nodes.getLon(i);
+ System.out.printf("Dense node, ID %d @ %.6f,%.6f\n",
+ lastId,parseLat(lastLat),parseLon(lastLon));
+ }
+ }
+
+ @Override
+ protected void parseNodes(List<Node> nodes) {
+ for (Node n : nodes) {
+ System.out.printf("Regular node, ID %d @ %.6f,%.6f\n",
+ n.getId(),parseLat(n.getLat()),parseLon(n.getLon()));
+ }
+ }
+
+ @Override
+ protected void parseWays(List<Way> ways) {
+ for (Way w : ways) {
+ System.out.println("Way ID " + w.getId());
+ StringBuilder sb = new StringBuilder();
+ sb.append(" Nodes: ");
+ long lastRef = 0;
+ for (Long ref : w.getRefsList()) {
+ lastRef+= ref;
+ sb.append(lastRef).append(" ");
+ }
+ sb.append("\n Key=value pairs: ");
+ for (int i=0 ; i<w.getKeysCount() ; i++) {
+ sb.append(getStringById(w.getKeys(i))).append("=")
+ .append(getStringById(w.getVals(i))).append(" ");
+ }
+ System.out.println(sb.toString());
+ }
+ }
+
+ @Override
+ protected void parse(HeaderBlock header) {
+ System.out.println("Got header block.");
+ }
+
+ public void complete() {
+ System.out.println("Complete!");
+ }
+
+ }
+
+}
--
Alioth's /usr/local/bin/git-commit-notice on /srv/git.debian.org/git/pkg-grass/osmpbf.git
More information about the Pkg-grass-devel
mailing list