source: josm/trunk/src/org/openstreetmap/josm/data/projection/datum/NTV2GridShiftFile.java@ 7392

Last change on this file since 7392 was 7082, checked in by Don-vip, 10 years ago

see #8465 - replace Utils.UTF_8 by StandardCharsets.UTF_8, new in Java 7

  • Property svn:eol-style set to native
File size: 12.4 KB
Line 
1/*
2 * Copyright (c) 2003 Objectix Pty Ltd All rights reserved.
3 *
4 * This library is free software; you can redistribute it and/or
5 * modify it under the terms of the GNU Lesser General Public
6 * License as published by the Free Software Foundation.
7 *
8 * THIS SOFTWARE IS PROVIDED "AS IS" AND ANY EXPRESSED OR IMPLIED
9 * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
10 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
11 * DISCLAIMED. IN NO EVENT SHALL OBJECTIX PTY LTD BE LIABLE FOR ANY
12 * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
13 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE
14 * GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
15 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
16 * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE
17 * OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE,
18 * EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
19 */
20package org.openstreetmap.josm.data.projection.datum;
21
22import java.io.IOException;
23import java.io.InputStream;
24import java.io.Serializable;
25import java.nio.charset.StandardCharsets;
26import java.util.ArrayList;
27import java.util.HashMap;
28import java.util.List;
29
30/**
31 * Models the NTv2 format Grid Shift File and exposes methods to shift
32 * coordinate values using the Sub Grids contained in the file.
33 * <p>The principal reference for the alogrithms used is the
34 * 'GDAit Software Architecture Manual' produced by the <a
35 * href='http://www.sli.unimelb.edu.au/gda94'>Geomatics
36 * Department of the University of Melbourne</a>
37 * <p>This library reads binary NTv2 Grid Shift files in Big Endian
38 * (Canadian standard) or Little Endian (Australian Standard) format.
39 * The older 'Australian' binary format is not supported, only the
40 * official Canadian format, which is now also used for the national
41 * Australian Grid.
42 * <p>Grid Shift files can be read as InputStreams or RandomAccessFiles.
43 * Loading an InputStream places all the required node information
44 * (accuracy data is optional) into heap based Java arrays. This is the
45 * highest perfomance option, and is useful for large volume transformations.
46 * Non-file data sources (eg using an SQL Blob) are also supported through
47 * InputStream. The RandonAccessFile option has a much smaller memory
48 * footprint as only the Sub Grid headers are stored in memory, but
49 * transformation is slower because the file must be read a number of
50 * times for each transformation.
51 * <p>Coordinates may be shifted Forward (ie from and to the Datums specified
52 * in the Grid Shift File header) or Reverse. The reverse transformation
53 * uses an iterative approach to approximate the Grid Shift, as the
54 * precise transformation is based on 'from' datum coordinates.
55 * <p>Coordinates may be specified
56 * either in Seconds using Positive West Longitude (the original NTv2
57 * arrangement) or in decimal Degrees using Positive East Longitude.
58 *
59 * @author Peter Yuill
60 * Modifified for JOSM :
61 * - removed the RandomAccessFile mode (Pieren)
62 */
63public class NTV2GridShiftFile implements Serializable {
64
65 private int overviewHeaderCount;
66 private int subGridHeaderCount;
67 private int subGridCount;
68 private String shiftType;
69 private String version;
70 private String fromEllipsoid = "";
71 private String toEllipsoid = "";
72 private double fromSemiMajorAxis;
73 private double fromSemiMinorAxis;
74 private double toSemiMajorAxis;
75 private double toSemiMinorAxis;
76
77 private NTV2SubGrid[] topLevelSubGrid;
78 private NTV2SubGrid lastSubGrid;
79
80 /**
81 * Constructs a new {@code NTV2GridShiftFile}.
82 */
83 public NTV2GridShiftFile() {
84 }
85
86 /**
87 * Load a Grid Shift File from an InputStream. The Grid Shift node
88 * data is stored in Java arrays, which will occupy about the same memory
89 * as the original file with accuracy data included, and about half that
90 * with accuracy data excluded. The size of the Australian national file
91 * is 4.5MB, and the Canadian national file is 13.5MB
92 * <p>The InputStream is closed by this method.
93 *
94 * @param in Grid Shift File InputStream
95 * @param loadAccuracy is Accuracy data to be loaded as well as shift data?
96 * @throws IOException
97 */
98 public void loadGridShiftFile(InputStream in, boolean loadAccuracy ) throws IOException {
99 byte[] b8 = new byte[8];
100 boolean bigEndian = true;
101 fromEllipsoid = "";
102 toEllipsoid = "";
103 topLevelSubGrid = null;
104 in.read(b8);
105 String overviewHeaderCountId = new String(b8, StandardCharsets.UTF_8);
106 if (!"NUM_OREC".equals(overviewHeaderCountId))
107 throw new IllegalArgumentException("Input file is not an NTv2 grid shift file");
108 in.read(b8);
109 overviewHeaderCount = NTV2Util.getIntBE(b8, 0);
110 if (overviewHeaderCount == 11) {
111 bigEndian = true;
112 } else {
113 overviewHeaderCount = NTV2Util.getIntLE(b8, 0);
114 if (overviewHeaderCount == 11) {
115 bigEndian = false;
116 } else
117 throw new IllegalArgumentException("Input file is not an NTv2 grid shift file");
118 }
119 in.read(b8);
120 in.read(b8);
121 subGridHeaderCount = NTV2Util.getInt(b8, bigEndian);
122 in.read(b8);
123 in.read(b8);
124 subGridCount = NTV2Util.getInt(b8, bigEndian);
125 NTV2SubGrid[] subGrid = new NTV2SubGrid[subGridCount];
126 in.read(b8);
127 in.read(b8);
128 shiftType = new String(b8, StandardCharsets.UTF_8);
129 in.read(b8);
130 in.read(b8);
131 version = new String(b8);
132 in.read(b8);
133 in.read(b8);
134 fromEllipsoid = new String(b8);
135 in.read(b8);
136 in.read(b8);
137 toEllipsoid = new String(b8);
138 in.read(b8);
139 in.read(b8);
140 fromSemiMajorAxis = NTV2Util.getDouble(b8, bigEndian);
141 in.read(b8);
142 in.read(b8);
143 fromSemiMinorAxis = NTV2Util.getDouble(b8, bigEndian);
144 in.read(b8);
145 in.read(b8);
146 toSemiMajorAxis = NTV2Util.getDouble(b8, bigEndian);
147 in.read(b8);
148 in.read(b8);
149 toSemiMinorAxis = NTV2Util.getDouble(b8, bigEndian);
150
151 for (int i = 0; i < subGridCount; i++) {
152 subGrid[i] = new NTV2SubGrid(in, bigEndian, loadAccuracy);
153 }
154 topLevelSubGrid = createSubGridTree(subGrid);
155 lastSubGrid = topLevelSubGrid[0];
156 }
157
158 /**
159 * Create a tree of Sub Grids by adding each Sub Grid to its parent (where
160 * it has one), and returning an array of the top level Sub Grids
161 * @param subGrid an array of all Sub Grids
162 * @return an array of top level Sub Grids with lower level Sub Grids set.
163 */
164 private NTV2SubGrid[] createSubGridTree(NTV2SubGrid[] subGrid) {
165 int topLevelCount = 0;
166 HashMap<String, List<NTV2SubGrid>> subGridMap = new HashMap<>();
167 for (int i = 0; i < subGrid.length; i++) {
168 if ("NONE".equalsIgnoreCase(subGrid[i].getParentSubGridName())) {
169 topLevelCount++;
170 }
171 subGridMap.put(subGrid[i].getSubGridName(), new ArrayList<NTV2SubGrid>());
172 }
173 NTV2SubGrid[] topLevelSubGrid = new NTV2SubGrid[topLevelCount];
174 topLevelCount = 0;
175 for (int i = 0; i < subGrid.length; i++) {
176 if ("NONE".equalsIgnoreCase(subGrid[i].getParentSubGridName())) {
177 topLevelSubGrid[topLevelCount++] = subGrid[i];
178 } else {
179 List<NTV2SubGrid> parent = subGridMap.get(subGrid[i].getParentSubGridName());
180 parent.add(subGrid[i]);
181 }
182 }
183 NTV2SubGrid[] nullArray = new NTV2SubGrid[0];
184 for (int i = 0; i < subGrid.length; i++) {
185 List<NTV2SubGrid> subSubGrids = subGridMap.get(subGrid[i].getSubGridName());
186 if (!subSubGrids.isEmpty()) {
187 NTV2SubGrid[] subGridArray = subSubGrids.toArray(nullArray);
188 subGrid[i].setSubGridArray(subGridArray);
189 }
190 }
191 return topLevelSubGrid;
192 }
193
194 /**
195 * Shift a coordinate in the Forward direction of the Grid Shift File.
196 *
197 * @param gs A GridShift object containing the coordinate to shift
198 * @return True if the coordinate is within a Sub Grid, false if not
199 */
200 public boolean gridShiftForward(NTV2GridShift gs) {
201 // Try the last sub grid first, big chance the coord is still within it
202 NTV2SubGrid subGrid = lastSubGrid.getSubGridForCoord(gs.getLonPositiveWestSeconds(), gs.getLatSeconds());
203 if (subGrid == null) {
204 subGrid = getSubGrid(gs.getLonPositiveWestSeconds(), gs.getLatSeconds());
205 }
206 if (subGrid == null)
207 return false;
208 else {
209 subGrid.interpolateGridShift(gs);
210 gs.setSubGridName(subGrid.getSubGridName());
211 lastSubGrid = subGrid;
212 return true;
213 }
214 }
215
216 /**
217 * Shift a coordinate in the Reverse direction of the Grid Shift File.
218 *
219 * @param gs A GridShift object containing the coordinate to shift
220 * @return True if the coordinate is within a Sub Grid, false if not
221 */
222 public boolean gridShiftReverse(NTV2GridShift gs) {
223 // set up the first estimate
224 NTV2GridShift forwardGs = new NTV2GridShift();
225 forwardGs.setLonPositiveWestSeconds(gs.getLonPositiveWestSeconds());
226 forwardGs.setLatSeconds(gs.getLatSeconds());
227 for (int i = 0; i < 4; i++) {
228 if (!gridShiftForward(forwardGs))
229 return false;
230 forwardGs.setLonPositiveWestSeconds(
231 gs.getLonPositiveWestSeconds() - forwardGs.getLonShiftPositiveWestSeconds());
232 forwardGs.setLatSeconds(gs.getLatSeconds() - forwardGs.getLatShiftSeconds());
233 }
234 gs.setLonShiftPositiveWestSeconds(-forwardGs.getLonShiftPositiveWestSeconds());
235 gs.setLatShiftSeconds(-forwardGs.getLatShiftSeconds());
236 gs.setLonAccuracyAvailable(forwardGs.isLonAccuracyAvailable());
237 if (forwardGs.isLonAccuracyAvailable()) {
238 gs.setLonAccuracySeconds(forwardGs.getLonAccuracySeconds());
239 }
240 gs.setLatAccuracyAvailable(forwardGs.isLatAccuracyAvailable());
241 if (forwardGs.isLatAccuracyAvailable()) {
242 gs.setLatAccuracySeconds(forwardGs.getLatAccuracySeconds());
243 }
244 return true;
245 }
246
247 /**
248 * Find the finest SubGrid containing the coordinate, specified
249 * in Positive West Seconds
250 *
251 * @param lon Longitude in Positive West Seconds
252 * @param lat Latitude in Seconds
253 * @return The SubGrid found or null
254 */
255 private NTV2SubGrid getSubGrid(double lon, double lat) {
256 NTV2SubGrid sub = null;
257 for (int i = 0; i < topLevelSubGrid.length; i++) {
258 sub = topLevelSubGrid[i].getSubGridForCoord(lon, lat);
259 if (sub != null) {
260 break;
261 }
262 }
263 return sub;
264 }
265
266 public boolean isLoaded() {
267 return (topLevelSubGrid != null);
268 }
269
270 public void unload() {
271 topLevelSubGrid = null;
272 }
273
274 @Override
275 public String toString() {
276 StringBuilder buf = new StringBuilder("Headers : ");
277 buf.append(overviewHeaderCount);
278 buf.append("\nSub Hdrs : ");
279 buf.append(subGridHeaderCount);
280 buf.append("\nSub Grids: ");
281 buf.append(subGridCount);
282 buf.append("\nType : ");
283 buf.append(shiftType);
284 buf.append("\nVersion : ");
285 buf.append(version);
286 buf.append("\nFr Ellpsd: ");
287 buf.append(fromEllipsoid);
288 buf.append("\nTo Ellpsd: ");
289 buf.append(toEllipsoid);
290 buf.append("\nFr Maj Ax: ");
291 buf.append(fromSemiMajorAxis);
292 buf.append("\nFr Min Ax: ");
293 buf.append(fromSemiMinorAxis);
294 buf.append("\nTo Maj Ax: ");
295 buf.append(toSemiMajorAxis);
296 buf.append("\nTo Min Ax: ");
297 buf.append(toSemiMinorAxis);
298 return buf.toString();
299 }
300
301 /**
302 * Get a copy of the SubGrid tree for this file.
303 *
304 * @return a deep clone of the current SubGrid tree
305 */
306 public NTV2SubGrid[] getSubGridTree() {
307 NTV2SubGrid[] clone = new NTV2SubGrid[topLevelSubGrid.length];
308 for (int i = 0; i < topLevelSubGrid.length; i++) {
309 clone[i] = (NTV2SubGrid)topLevelSubGrid[i].clone();
310 }
311 return clone;
312 }
313
314 public String getFromEllipsoid() {
315 return fromEllipsoid;
316 }
317
318 public String getToEllipsoid() {
319 return toEllipsoid;
320 }
321
322}
Note: See TracBrowser for help on using the repository browser.