source: osm/applications/editors/josm/plugins/pbf/src/crosby/binary/BinarySerializer.java@ 28362

Last change on this file since 28362 was 26961, checked in by donvip, 15 years ago

JOSM PBF plugin

File size: 5.3 KB
Line 
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
18package crosby.binary;
19
20import java.io.IOException;
21import java.util.ArrayList;
22import java.util.List;
23
24import crosby.binary.Osmformat.PrimitiveGroup;
25import crosby.binary.file.BlockOutputStream;
26import crosby.binary.file.FileBlock;
27
28/**
29 * Generic serializer common code
30 *
31 * Serialize a set of blobs and process them. Subclasses implement handlers for
32 * different API's (osmosis, mkgmap, splitter, etc.)
33 *
34 * All data is converted into PrimGroupWriterInterface objects, which are then
35 * ordered to process their data at the appropriate time.
36 * */
37
38public class BinarySerializer {
39
40 /**
41 * Interface used to write a group of primitives. One of these for each
42 * group type (Node, Way, Relation, DenseNode, Changeset)
43 */
44 protected interface PrimGroupWriterInterface {
45 /** This callback is invoked on each group that is going into the fileblock in order to give it a chance to
46 * add to the stringtable pool of strings. */
47 public void addStringsToStringtable();
48
49 /**
50 * This callback is invoked to request that the primgroup serialize itself into the given protocol buffer object.
51 */
52 public Osmformat.PrimitiveGroup serialize();
53 }
54
55 /** Set the granularity (precision of lat/lon, measured in unites of nanodegrees. */
56 public void configGranularity(int granularity) {
57 this.granularity = granularity;
58 }
59
60 /** Set whether metadata is to be omitted */
61 public void configOmit(boolean omit_metadata) {
62 this.omit_metadata = omit_metadata;
63 }
64
65 /** Configure the maximum number of entities in a batch */
66 public void configBatchLimit(int batch_limit) {
67 this.batch_limit = batch_limit;
68 }
69
70 // Paramaters affecting the output size.
71 protected final int MIN_DENSE = 10;
72 protected int batch_limit = 4000;
73
74 // Parmaters affecting the output.
75
76 protected int granularity = 100;
77 protected int date_granularity = 1000;
78 protected boolean omit_metadata = false;
79
80 /** How many primitives have been seen in this batch */
81 protected int batch_size = 0;
82 protected int total_entities = 0;
83 private StringTable stringtable = new StringTable();
84 protected List<PrimGroupWriterInterface> groups = new ArrayList<PrimGroupWriterInterface>();
85 protected BlockOutputStream output;
86
87 public BinarySerializer(BlockOutputStream output) {
88 this.output = output;
89 }
90
91 public StringTable getStringTable() {
92 return stringtable;
93 }
94
95 public void flush() throws IOException {
96 processBatch();
97 output.flush();
98 }
99
100 public void close() throws IOException {
101 flush();
102 output.close();
103 }
104
105 long debug_bytes = 0;
106
107 public void processBatch() {
108 // System.out.format("Batch of %d groups: ",groups.size());
109 if (groups.size() == 0)
110 return;
111 Osmformat.PrimitiveBlock.Builder primblock = Osmformat.PrimitiveBlock
112 .newBuilder();
113 stringtable.clear();
114 // Preprocessing: Figure out the stringtable.
115 for (PrimGroupWriterInterface i : groups)
116 i.addStringsToStringtable();
117
118 stringtable.finish();
119 // Now, start serializing.
120 for (PrimGroupWriterInterface i : groups) {
121 PrimitiveGroup group = i.serialize();
122 if (group != null)
123 primblock.addPrimitivegroup(group);
124 }
125 primblock.setStringtable(stringtable.serialize());
126 primblock.setGranularity(this.granularity);
127 primblock.setDateGranularity(this.date_granularity);
128
129 // Only generate data with offset (0,0)
130 //
131 Osmformat.PrimitiveBlock message = primblock.build();
132
133 // System.out.println(message);
134 debug_bytes += message.getSerializedSize();
135 // if (message.getSerializedSize() > 1000000)
136 // System.out.println(message);
137
138 try {
139 output.write(FileBlock.newInstance("OSMData", message
140 .toByteString(), null));
141 } catch (IOException e) {
142 // TODO Auto-generated catch block
143 e.printStackTrace();
144 throw new Error(e);
145 } finally {
146 batch_size = 0;
147 groups.clear();
148 }
149 // System.out.format("\n");
150 }
151
152 /** Convert from a degrees represented as a double into the serialized offset in nanodegrees.. */
153 public long mapRawDegrees(double degrees) {
154 return (long) ((degrees / .000000001));
155 }
156
157 /** Convert from a degrees represented as a double into the serialized offset. */
158 public int mapDegrees(double degrees) {
159 return (int) ((degrees / .0000001) / (granularity / 100));
160 }
161}
Note: See TracBrowser for help on using the repository browser.