| 1 | /** Copyright (c) 2010 Scott A. Crosby. <scott@sacrosby.com>
|
|---|
| 2 |
|
|---|
| 3 | This program is free software: you can redistribute it and/or modify
|
|---|
| 4 | it under the terms of the GNU Lesser General Public License as
|
|---|
| 5 | published by the Free Software Foundation, either version 3 of the
|
|---|
| 6 | License, or (at your option) any later version.
|
|---|
| 7 |
|
|---|
| 8 | This program is distributed in the hope that it will be useful,
|
|---|
| 9 | but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|---|
| 10 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|---|
| 11 | GNU General Public License for more details.
|
|---|
| 12 |
|
|---|
| 13 | You should have received a copy of the GNU General Public License
|
|---|
| 14 | along with this program. If not, see <http://www.gnu.org/licenses/>.
|
|---|
| 15 |
|
|---|
| 16 | */
|
|---|
| 17 |
|
|---|
| 18 | package crosby.binary;
|
|---|
| 19 |
|
|---|
| 20 |
|
|---|
| 21 | import java.util.Date;
|
|---|
| 22 | import java.util.List;
|
|---|
| 23 |
|
|---|
| 24 | import com.google.protobuf.InvalidProtocolBufferException;
|
|---|
| 25 |
|
|---|
| 26 | import crosby.binary.Osmformat;
|
|---|
| 27 | import crosby.binary.file.BlockReaderAdapter;
|
|---|
| 28 | import crosby.binary.file.FileBlock;
|
|---|
| 29 | import crosby.binary.file.FileBlockPosition;
|
|---|
| 30 |
|
|---|
| 31 | public abstract class BinaryParser implements BlockReaderAdapter {
|
|---|
| 32 | protected int granularity;
|
|---|
| 33 | private long lat_offset;
|
|---|
| 34 | private long lon_offset;
|
|---|
| 35 | protected int date_granularity;
|
|---|
| 36 | private String strings[];
|
|---|
| 37 |
|
|---|
| 38 | /** Take a Info protocol buffer containing a date and convert it into a java Date object */
|
|---|
| 39 | protected Date getDate(Osmformat.Info info) {
|
|---|
| 40 | if (info.hasTimestamp()) {
|
|---|
| 41 | return new Date(date_granularity * (long) info.getTimestamp());
|
|---|
| 42 | } else
|
|---|
| 43 | return NODATE;
|
|---|
| 44 | }
|
|---|
| 45 | public static final Date NODATE = new Date(-1);
|
|---|
| 46 |
|
|---|
| 47 | /** Get a string based on the index used.
|
|---|
| 48 | *
|
|---|
| 49 | * Index 0 is reserved to use as a delimiter, therefore, index 1 corresponds to the first string in the table
|
|---|
| 50 | * @param id
|
|---|
| 51 | * @return
|
|---|
| 52 | */
|
|---|
| 53 | protected String getStringById(int id) {
|
|---|
| 54 | return strings[id];
|
|---|
| 55 | }
|
|---|
| 56 |
|
|---|
| 57 | //@Override
|
|---|
| 58 | public void handleBlock(FileBlock message) {
|
|---|
| 59 | // TODO Auto-generated method stub
|
|---|
| 60 | try {
|
|---|
| 61 | if (message.getType().equals("OSMHeader")) {
|
|---|
| 62 | Osmformat.HeaderBlock headerblock = Osmformat.HeaderBlock
|
|---|
| 63 | .parseFrom(message.getData());
|
|---|
| 64 | parse(headerblock);
|
|---|
| 65 | } else if (message.getType().equals("OSMData")) {
|
|---|
| 66 | Osmformat.PrimitiveBlock primblock = Osmformat.PrimitiveBlock
|
|---|
| 67 | .parseFrom(message.getData());
|
|---|
| 68 | parse(primblock);
|
|---|
| 69 | }
|
|---|
| 70 | } catch (InvalidProtocolBufferException e) {
|
|---|
| 71 | // TODO Auto-generated catch block
|
|---|
| 72 | e.printStackTrace();
|
|---|
| 73 | throw new Error("ParseError"); // TODO
|
|---|
| 74 | }
|
|---|
| 75 |
|
|---|
| 76 | }
|
|---|
| 77 |
|
|---|
| 78 |
|
|---|
| 79 | //@Override
|
|---|
| 80 | public boolean skipBlock(FileBlockPosition block) {
|
|---|
| 81 | // System.out.println("Seeing block of type: "+block.getType());
|
|---|
| 82 | if (block.getType().equals("OSMData"))
|
|---|
| 83 | return false;
|
|---|
| 84 | if (block.getType().equals("OSMHeader"))
|
|---|
| 85 | return false;
|
|---|
| 86 | System.out.println("Skipped block of type: " + block.getType());
|
|---|
| 87 | return true;
|
|---|
| 88 | }
|
|---|
| 89 |
|
|---|
| 90 |
|
|---|
| 91 | /** Convert a latitude value stored in a protobuf into a double, compensating for granularity and latitude offset */
|
|---|
| 92 | public double parseLat(long degree) {
|
|---|
| 93 | // Support non-zero offsets. (We don't currently generate them)
|
|---|
| 94 | return (granularity * degree + lat_offset) * .000000001;
|
|---|
| 95 | }
|
|---|
| 96 |
|
|---|
| 97 | /** Convert a longitude value stored in a protobuf into a double, compensating for granularity and longitude offset */
|
|---|
| 98 | public double parseLon(long degree) {
|
|---|
| 99 | // Support non-zero offsets. (We don't currently generate them)
|
|---|
| 100 | return (granularity * degree + lon_offset) * .000000001;
|
|---|
| 101 | }
|
|---|
| 102 |
|
|---|
| 103 | /** Parse a Primitive block (containing a string table, other paramaters, and PrimitiveGroups */
|
|---|
| 104 | public void parse(Osmformat.PrimitiveBlock block) {
|
|---|
| 105 | Osmformat.StringTable stablemessage = block.getStringtable();
|
|---|
| 106 | strings = new String[stablemessage.getSCount()];
|
|---|
| 107 |
|
|---|
| 108 | for (int i = 0; i < strings.length; i++) {
|
|---|
| 109 | strings[i] = stablemessage.getS(i).toStringUtf8();
|
|---|
| 110 | }
|
|---|
| 111 |
|
|---|
| 112 | granularity = block.getGranularity();
|
|---|
| 113 | lat_offset = block.getLatOffset();
|
|---|
| 114 | lon_offset = block.getLonOffset();
|
|---|
| 115 | date_granularity = block.getDateGranularity();
|
|---|
| 116 |
|
|---|
| 117 | for (Osmformat.PrimitiveGroup groupmessage : block
|
|---|
| 118 | .getPrimitivegroupList()) {
|
|---|
| 119 | // Exactly one of these should trigger on each loop.
|
|---|
| 120 | parseNodes(groupmessage.getNodesList());
|
|---|
| 121 | parseWays(groupmessage.getWaysList());
|
|---|
| 122 | parseRelations(groupmessage.getRelationsList());
|
|---|
| 123 | if (groupmessage.hasDense())
|
|---|
| 124 | parseDense(groupmessage.getDense());
|
|---|
| 125 | }
|
|---|
| 126 | }
|
|---|
| 127 |
|
|---|
| 128 | /** Parse a list of Relation protocol buffers and send the resulting relations to a sink. */
|
|---|
| 129 | protected abstract void parseRelations(List<Osmformat.Relation> rels);
|
|---|
| 130 | /** Parse a DenseNode protocol buffer and send the resulting nodes to a sink. */
|
|---|
| 131 | protected abstract void parseDense(Osmformat.DenseNodes nodes);
|
|---|
| 132 | /** Parse a list of Node protocol buffers and send the resulting nodes to a sink. */
|
|---|
| 133 | protected abstract void parseNodes(List<Osmformat.Node> nodes);
|
|---|
| 134 | /** Parse a list of Way protocol buffers and send the resulting ways to a sink. */
|
|---|
| 135 | protected abstract void parseWays(List<Osmformat.Way> ways);
|
|---|
| 136 | /** Parse a header message. */
|
|---|
| 137 | protected abstract void parse(Osmformat.HeaderBlock header);
|
|---|
| 138 |
|
|---|
| 139 | }
|
|---|