Revision: 5010
http://sourceforge.net/p/jump-pilot/code/5010
Author: michaudm
Date: 2016-09-10 09:47:35 +0000 (Sat, 10 Sep 2016)
Log Message:
-----------
A class to represent SRS Information (code and unit, usable for information,
not for transformation)
Added Paths:
-----------
core/trunk/src/org/openjump/core/ccordsys/utils/SRSInfo.java
Added: core/trunk/src/org/openjump/core/ccordsys/utils/SRSInfo.java
===================================================================
--- core/trunk/src/org/openjump/core/ccordsys/utils/SRSInfo.java
(rev 0)
+++ core/trunk/src/org/openjump/core/ccordsys/utils/SRSInfo.java
2016-09-10 09:47:35 UTC (rev 5010)
@@ -0,0 +1,152 @@
+package org.openjump.core.ccordsys.utils;
+
+import java.io.UnsupportedEncodingException;
+
+import static org.openjump.core.ccordsys.utils.SRSInfo.Registry.EPSG;
+import static org.openjump.core.ccordsys.utils.SRSInfo.Registry.ESRI;
+import static org.openjump.core.ccordsys.utils.SRSInfo.Registry.SRID;
+import static org.openjump.core.ccordsys.utils.SRSInfo.Unit.UNKNOWN;
+
+/**
+ * Small container for SRS information.
+ * This class does not contains all information to perform coordinate
transformation,
+ * but enough to return metadata about SRS code or map unit
+ */
+public class SRSInfo {
+
+ public static final String UNDEFINED = "0";
+ public static final String USERDEFINED = "32767";
+
+ public enum Registry{SRID, EPSG, ESRI, IGNF, SRORG}
+
+ public enum Unit {UNKNOWN, METER, FOOT, YARD, MILE, RADIAN, DEGREE, GRADE,
DMS, DMSH}
+
+ private String source; // The source of SRS information (ex.
prj file path)
+ private Registry registry = EPSG; // The registry in which this SRS is
referenced
+ private String code = UNDEFINED; // The code of the SRS
+ private String description = ""; // The name or description of the SRS
+ private Unit unit = UNKNOWN; // The unit used by this SRS
+
+ public SRSInfo() {}
+
+ public String getSource() {
+ return source;
+ }
+
+ public SRSInfo setSource(String source) {
+ this.source = source;
+ return this;
+ }
+
+ public Registry getRegistry() {
+ if (code == null && description == null)
+ throw new IllegalStateException("SRSInfo must have a code or a
description");
+ if (registry != null) return registry;
+ else return guessRegistry(getCode());
+ }
+
+ public SRSInfo setRegistry(Registry registry) {
+ this.registry = registry;
+ return this;
+ }
+
+ public SRSInfo setRegistry(String registry) {
+ this.registry = Registry.valueOf(registry);
+ return this;
+ }
+
+ public String getCode() {
+ if (code == null && description == null)
+ throw new IllegalStateException("SRSInfo must have a code or a
description");
+ return code;
+ }
+
+ public SRSInfo setCode(String code) {
+ this.code = code;
+ return this;
+ }
+
+ public String getDescription() {
+ if (code == null && description == null)
+ throw new IllegalStateException("SRSInfo must have a code or a
description");
+ return description;
+ }
+
+ public SRSInfo setDescription(String description) {
+ this.description = description;
+ return this;
+ }
+
+ public Unit getUnit() {
+ if (code == null && description == null)
+ throw new IllegalStateException("SRSInfo must have a code or a
description");
+ return unit;
+ }
+
+ public SRSInfo setUnit(Unit unit) {
+ this.unit = unit;
+ return this;
+ }
+
+ public SRSInfo setUnit(String unit) {
+ this.unit = Unit.valueOf(unit);
+ return this;
+ }
+
+ public void complete() throws UnsupportedEncodingException {
+ if (description.isEmpty()) {
+ setDescription(SridLookupTable.getSrsNameFromCode(code));
+ }
+ if (code.isEmpty() || code.equals(UNDEFINED)) {
+ setCode(SridLookupTable.getSrsCodeFromName(description));
+ }
+ registry = guessRegistry(code);
+ }
+
+ @Override
+ public String toString() {
+ return getRegistry().toString() + ':' + getCode() + ' ' +
getDescription() + '[' + getUnit() + ']';
+ }
+
+ @Override
+ public boolean equals(Object o) {
+ if (this == o) return true;
+ if (o == null || getClass() != o.getClass()) return false;
+
+ SRSInfo srsInfo = (SRSInfo) o;
+
+ if (registry != null ? !registry.equals(srsInfo.registry) :
srsInfo.registry != null) return false;
+ if (code != null ? !code.equals(srsInfo.code) : srsInfo.code != null)
return false;
+ if (description != null ? !description.equals(srsInfo.description) :
srsInfo.description != null) return false;
+ return unit != null ? unit.equals(srsInfo.unit) : srsInfo.unit == null;
+
+ }
+
+ @Override
+ public int hashCode() {
+ int result = registry != null ? registry.hashCode() : 0;
+ result = 31 * result + (code != null ? code.hashCode() : 0);
+ result = 31 * result + (description != null ? description.hashCode() :
0);
+ result = 31 * result + (unit != null ? unit.hashCode() : 0);
+ return result;
+ }
+
+ private static Registry guessRegistry(String code) {
+ // 1) WKID <32768 or >5999999
+ // will result in an AUTHORITY name of "EPSG".
+ // 2) A WKID in range between 33000 and 199999
+ // will result in an AUTHORITY name of "ESRI".
+ //
(http://help.arcgis.com/en/arcgisserver/10.0/apis/soap/whnjs.htm#SOAP_Geometry_FindSRByWKID.htm)
+
+ Registry registry = SRID; // Used if code is negative or non-numeric
+ if (code != null && code.matches("\\d+")) {
+ int srid = Integer.parseInt(code);
+ if (srid < 32768 || srid > 5999999) {
+ registry = EPSG;
+ } else if (srid > 32999 && srid < 200000) {
+ registry = ESRI;
+ }
+ }
+ return registry;
+ }
+}
------------------------------------------------------------------------------
_______________________________________________
Jump-pilot-devel mailing list
[email protected]
https://lists.sourceforge.net/lists/listinfo/jump-pilot-devel