Skip to content

Commit 06ebc68

Browse files
committed
Add a unit test plus a method to get elements from a PRJ string. Useful to parse OGC WKT2 format
1 parent eab9043 commit 06ebc68

File tree

4 files changed

+66
-4
lines changed

4 files changed

+66
-4
lines changed

docs/CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,3 +4,4 @@
44
- Remove unnecessary logger
55
- Update github actions
66
- Change to central portal publishing
7+
- Add a new method to return the elements from a PRJ string

src/main/java/org/cts/parser/prj/PrjParser.java

Lines changed: 19 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -50,24 +50,39 @@ public PrjParser() {
5050
}
5151

5252
/**
53-
* Parses a WKT PRJ String into a set of parameters.
53+
* Parses a WKT PRJ String into a {@code PrjElement}.
54+
*
5455
* <p>
5556
* This is the main entry point of the parser.
5657
*
5758
* @param prjString a WKT string
58-
* @return a list of parameters
59+
* @return a {@code PrjElement}
5960
* @throws PrjParserException if the PRJ cannot be parsed into a CRS for any
6061
* reason
6162
*/
62-
public Map<String, String> getParameters(String prjString) {
63+
public PrjElement getAsPrjElement(String prjString) {
6364
CharBuffer s = CharBuffer.wrap(prjString);
6465
PrjElement e;
6566
try {
6667
e = parseNode(s);
6768
} catch (BufferUnderflowException ex) {
6869
throw new PrjParserException("Failed to read PRJ.", ex);
6970
}
70-
return PrjMatcher.match(e);
71+
return e;
72+
}
73+
74+
/**
75+
* Parses a WKT PRJ String into a set of parameters.
76+
* <p>
77+
* This is the main entry point of the parser.
78+
*
79+
* @param prjString a WKT string
80+
* @return a list of parameters
81+
* @throws PrjParserException if the PRJ cannot be parsed into a CRS for any
82+
* reason
83+
*/
84+
public Map<String, String> getParameters(String prjString) {
85+
return PrjMatcher.match(getAsPrjElement(prjString));
7186
}
7287

7388
/**

src/test/java/org/cts/op/EPSGTransformTest.java

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -166,6 +166,20 @@ void testEPSGCodeFrom2100To4326() throws Exception {
166166
assertTrue(checkEquals2D("EPSG:2100 to EPSG:4326 ", result, pointDest, tolerance));
167167
}
168168

169+
@Test
170+
void testEPSGCodeFrom27700To4326() throws Exception {
171+
String csNameSrc = "EPSG:27700"; //Input EPSG
172+
double[] pointSource = new double[]{489581,-55232};
173+
String csNameDest = "EPSG:4326"; //Target EPSG
174+
double[] pointDest = new double[]{-0.7667563514593325, 49.396886444795};
175+
double tolerance = 0.01;
176+
CoordinateReferenceSystem inputCRS = cRSFactory.getCRS(csNameSrc);
177+
CoordinateReferenceSystem outputCRS = cRSFactory.getCRS(csNameDest);
178+
verbose = false;
179+
double[] result = transform((GeodeticCRS) inputCRS, (GeodeticCRS) outputCRS, pointSource);
180+
assertTrue(checkEquals2D("EPSG:27700 to EPSG:4326 ", result, pointDest, tolerance));
181+
}
182+
169183

170184
//TODO : Fix me add support to NADGRID
171185
@Disabled

src/test/java/org/cts/parser/prj/PrjParserTest.java

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@
2828
import java.io.RandomAccessFile;
2929
import java.net.URI;
3030
import java.nio.CharBuffer;
31+
import java.util.List;
3132
import java.util.Map;
3233

3334
import org.cts.CRSFactory;
@@ -319,4 +320,35 @@ void testCompareCRS_PRJ() throws Exception {
319320
assertNotNull(crs);
320321
assertFalse(crs.equals(crs2));
321322
}
323+
324+
@Test
325+
void testWKT2() {
326+
String prj = "PROJCRS[\"WGS 84 / UTM zone 31N\",\n" +
327+
"BASEGEOGCRS[\"WGS 84\",ENSEMBLE[\"World Geodetic System 1984 ensemble\",MEMBER[\"World Geodetic System 1984 (Transit)\"],MEMBER[\"World Geodetic System 1984 (G730)\"],MEMBER[\"World Geodetic System 1984 (G873)\"],MEMBER[\"World Geodetic System 1984 (G1150)\"],MEMBER[\"World Geodetic System 1984 (G1674)\"],MEMBER[\"World Geodetic System 1984 (G1762)\"],MEMBER[\"World Geodetic System 1984 (G2139)\"],MEMBER[\"World Geodetic System 1984 (G2296)\"],ELLIPSOID[\"WGS 84\",6378137,298.257223563,LENGTHUNIT[\"metre\",1]],ENSEMBLEACCURACY[2.0]],PRIMEM[\"Greenwich\",0,ANGLEUNIT[\"degree\",0.0174532925199433]],ID[\"EPSG\",4326]],\n" +
328+
"CONVERSION[\"UTM zone 31N\",METHOD[\"Transverse Mercator\",ID[\"EPSG\",9807]],PARAMETER[\"Latitude of natural origin\",0,ANGLEUNIT[\"degree\",0.0174532925199433],ID[\"EPSG\",8801]],PARAMETER[\"Longitude of natural origin\",3,ANGLEUNIT[\"degree\",0.0174532925199433],ID[\"EPSG\",8802]],PARAMETER[\"Scale factor at natural origin\",0.9996,SCALEUNIT[\"unity\",1],ID[\"EPSG\",8805]],PARAMETER[\"False easting\",500000,LENGTHUNIT[\"metre\",1],ID[\"EPSG\",8806]],PARAMETER[\"False northing\",0,LENGTHUNIT[\"metre\",1],ID[\"EPSG\",8807]]],\n" +
329+
"CS[Cartesian,2],\n" +
330+
"AXIS[\"(E)\",east,ORDER[1],LENGTHUNIT[\"metre\",1]],\n" +
331+
"AXIS[\"(N)\",north,ORDER[2],LENGTHUNIT[\"metre\",1]],\n" +
332+
"USAGE[SCOPE[\"Navigation and medium accuracy spatial referencing.\"],AREA[\"Between 0°E and 6°E, northern hemisphere between equator and 84°N, onshore and offshore. Algeria. Andorra. Belgium. Benin. Burkina Faso. Denmark - North Sea. France. Germany - North Sea. Ghana. Luxembourg. Mali. Netherlands. Niger. Nigeria. Norway. Spain. Togo. United Kingdom (UK) - North Sea.\"],BBOX[0,0,84,6]],\n" +
333+
"ID[\"EPSG\",32631]]";
334+
PrjElement prjElement = parser.getAsPrjElement(prj);
335+
assertInstanceOf(PrjNodeElement.class, prjElement);
336+
PrjNodeElement firstNode = (PrjNodeElement) prjElement;
337+
assertEquals("PROJCRS", firstNode.getName());
338+
List<PrjElement> nodes = firstNode.getChildren();
339+
PrjNodeElement id_epsg=null;
340+
for (PrjElement node : nodes) {
341+
if(node instanceof PrjNodeElement) {
342+
id_epsg = (PrjNodeElement) node;
343+
if (id_epsg.getName().equals("ID")) {
344+
break;
345+
}
346+
}
347+
}
348+
assertEquals("ID", id_epsg.getName());
349+
List<PrjElement> epsgElement = id_epsg.getChildren();
350+
assertEquals(2, epsgElement.size());
351+
assertEquals("EPSG", ((PrjStringElement)epsgElement.get(0)).getValue());
352+
assertEquals(32631, ((PrjNumberElement)epsgElement.get(1)).getValue());
353+
}
322354
}

0 commit comments

Comments
 (0)