Skip to content

Commit

Permalink
Merge pull request orbisgis#123 from ebocher/findUTMZone
Browse files Browse the repository at this point in the history
UTM zone finder + proj4 parser
  • Loading branch information
ebocher authored Aug 30, 2019
2 parents 52a841c + 01ea60b commit 1bced33
Show file tree
Hide file tree
Showing 12 changed files with 644 additions and 4 deletions.
1 change: 1 addition & 0 deletions .travis.yml
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
dist: trusty
language: java
jdk:
- oraclejdk8
Expand Down
2 changes: 1 addition & 1 deletion pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
<parent>
<groupId>org.orbisgis</groupId>
<artifactId>orbisparent</artifactId>
<version>1.0.1</version>
<version>1.0.3-SNAPSHOT</version>
</parent>

<artifactId>cts</artifactId>
Expand Down
34 changes: 33 additions & 1 deletion src/main/java/org/cts/CRSFactory.java
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@

import java.io.*;
import java.nio.charset.Charset;
import java.util.Date;
import java.util.LinkedHashMap;
import java.util.Map;
import java.util.Set;
Expand All @@ -33,6 +34,7 @@
import org.cts.crs.CoordinateReferenceSystem;
import org.cts.parser.prj.PrjKeyParameters;
import org.cts.parser.prj.PrjParser;
import org.cts.parser.proj4.Proj4Parser;
import org.cts.registry.Registry;
import org.cts.registry.RegistryException;
import org.cts.registry.RegistryManager;
Expand Down Expand Up @@ -168,7 +170,9 @@ public CoordinateReferenceSystem createFromPrj(String prjString) throws CRSExcep
*
* @param stream the input stream of bytes defining the OGC WKT String
* @param encoding the charset used to read the input stream
* @return a CoordinateReferenceSystem
* @throws IOException
* @throws org.cts.crs.CRSException
*/
public CoordinateReferenceSystem createFromPrj(InputStream stream, Charset encoding) throws IOException, CRSException {
BufferedReader r = new BufferedReader(new InputStreamReader(stream, encoding));
Expand All @@ -184,7 +188,9 @@ public CoordinateReferenceSystem createFromPrj(InputStream stream, Charset encod
* (PRJ).
*
* @param stream the input stream of bytes defining the OGC WKT String
* @return a CoordinateReferenceSystem
* @throws IOException
* @throws org.cts.crs.CRSException
*/
public CoordinateReferenceSystem createFromPrj(InputStream stream) throws IOException, CRSException {
return createFromPrj(stream, Charset.defaultCharset());
Expand All @@ -193,9 +199,11 @@ public CoordinateReferenceSystem createFromPrj(InputStream stream) throws IOExce
/**
* Creates a {@link CoordinateReferenceSystem} defined by an OGC WKT String
* (PRJ).
*
* @return a CoordinateReferenceSystem
* @param file containing the OGC WKT String that defined the desired CRS
* @return
* @throws IOException if there is a problem reading the file
* @throws org.cts.crs.CRSException
*/
public CoordinateReferenceSystem createFromPrj(File file) throws IOException, CRSException {
InputStream i = null;
Expand All @@ -215,10 +223,34 @@ public CoordinateReferenceSystem createFromPrj(File file) throws IOException, CR
* Return a list of supported codes according an registryName.
*
* @param registryName (ex : EPSG, IGNF, ESRI)
* @return List of supported codes
* @throws org.cts.registry.RegistryException
*/
public Set<String> getSupportedCodes(String registryName) throws RegistryException {
return getRegistryManager().getRegistry(registryName).getSupportedCodes();
}


/**
* Creates a {@link CoordinateReferenceSystem} defined by a proj4 string
* representation
*
* @param prj4String the proj4 string defining the CRS
* @return
* @throws org.cts.crs.CRSException
*/
public CoordinateReferenceSystem createFromPrj4(String prj4String) throws CRSException {
Map<String, String> prjParameters = Proj4Parser.readParameters(prj4String);
String zone = prjParameters.get("zone");
String crsName;
if (zone != null) {
crsName = prjParameters.get("south") == null ? String.format("UTM %s %s", zone, "NORTH") : String.format("UTM %s %s", zone, "SOUTH");
}
else{
crsName = String.format("Unknown CRS %s",System.currentTimeMillis());
}
return CRSHelper.createCoordinateReferenceSystem(new Identifier(CoordinateReferenceSystem.class, crsName), prjParameters);
}

/**
* A simple cache to manage {@link CoordinateReferenceSystem}
Expand Down
3 changes: 3 additions & 0 deletions src/main/java/org/cts/CRSHelper.java
Original file line number Diff line number Diff line change
Expand Up @@ -767,6 +767,9 @@ private static Projection getProjection(String projectionName, Ellipsoid ell,
}
}




/**
* A simple cache to manage {@link AbstractCoordinateOperation}
*/
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,6 @@
import org.cts.CoordinateDimensionException;
import org.cts.Identifier;
import org.cts.datum.Ellipsoid;
import org.cts.op.NonInvertibleOperationException;
import org.cts.units.Measure;
import org.cts.util.Complex;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,6 @@
import org.cts.Identifier;
import org.cts.Parameter;
import org.cts.datum.Ellipsoid;
import org.cts.op.NonInvertibleOperationException;
import org.cts.units.Measure;
import org.cts.units.Unit;
import org.cts.util.Complex;
Expand Down
86 changes: 86 additions & 0 deletions src/main/java/org/cts/parser/proj4/Proj4Parser.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,86 @@
/*
* Coordinate Transformations Suite (abridged CTS) is a library developped to
* perform Coordinate Transformations using well known geodetic algorithms
* and parameter sets.
* Its main focus are simplicity, flexibility, interoperability, in this order.
*
* This library has been originally developed by Michaël Michaud under the JGeod
* name. It has been renamed CTS in 2009 and shared to the community from
* the OrbisGIS code repository.
*
* CTS is free software: you can redistribute it and/or modify it under the
* terms of the GNU Lesser General Public License as published by the Free Software
* Foundation, either version 3 of the License.
*
* CTS is distributed in the hope that it will be useful, but WITHOUT ANY
* WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
* A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public License along with
* CTS. If not, see <http://www.gnu.org/licenses/>.
*
* For more information, please consult: <https://github.com/orbisgis/cts/>
*/
package org.cts.parser.proj4;

import java.util.HashMap;
import java.util.Map;
import java.util.regex.Pattern;
import org.cts.parser.proj.ProjKeyParameters;

/**
* A simple Proj4 parser to return a list of parameters used to build a
* CoordinateSystem
*
* @author Erwan Bocher, CNRS
*/
public class Proj4Parser {

/**
* The regex that must be used to parse.
*/
static final Pattern regex = Pattern.compile("[ ]\\+|\\s<>");

public static Map<String, String> readParameters(String projText) {
if (projText == null || projText.isEmpty()) {
throw new IllegalArgumentException("Please set a correct proj4 representation");
}
Map<String, String> params = new HashMap<String, String>();

String[] tokens = regex.split(projText);

if (tokens[0].startsWith("+proj")) {
for (String token : tokens) {
String[] keyValue = token.split("=");
if (keyValue.length == 2) {
String key = formatKey(keyValue[0]);
ProjKeyParameters.checkUnsupported(key);
params.put(key, keyValue[1]);
} else {
String key = formatKey(token);
ProjKeyParameters.checkUnsupported(key);
params.put(key, null);
}
}
return params;
} else {
throw new IllegalArgumentException("The proj4 representation must startwith +proj");
}
}

/**
* Remove + char if exists
*
* @param key
*/
private static String formatKey(String key) {
String formatKey = key;
if (key.startsWith("+")) {
formatKey = key.substring(1);
}
return formatKey;
}



}
Loading

0 comments on commit 1bced33

Please sign in to comment.