org.geolatte.geom.crs.CrsId Maven / Gradle / Ivy
Show all versions of geolatte-geom Show documentation
package org.geolatte.geom.crs;
import java.io.Serializable;
/**
* An identifier for a CoordinateReferenceSystem
or other object referenced in a
* CoordinateReferenceSystem
definition.
*
* A commonly used alternative name for CrsId
is "SRID".
*
* A CrsId
consists of the name of an authority and a numeric identifier. The authority is the
* organization that is responsible for managing the definition of the identified object.
*
* In practice, CoordinateReferenceSystem
s are usually identified by their EPSG code, with EPSG
* the authority for the identifier. In this implementation, EPSG is the default authority.
*
* @author Karel Maesen, Geovise BVBA, 2011
*/
public class CrsId implements Serializable {
private static final long serialVersionUID = 6884205871950410216L;
final static public String DEFAULT_AUTHORITY = "EPSG";
/**
* Identifies an undefined (or unknown) CrsId
.
*/
final static public CrsId UNDEFINED = new CrsId("EPSG", -1);
final private String authority;
final private int code;
/**
* Creates an instance from a String
of the form "[<authority>:]<code>".
*
* If the "authority" prefix is missing, then the authority will be assumed to be EPSG.
*
* @param srsString the string to parse into a CrsId
.
* @return
* @throws IllegalArgumentException when the string can nog be parsed as
*/
static public CrsId parse(String srsString) {
if (srsString == null || srsString.isEmpty())
throw new IllegalArgumentException("Require input of form ':");
String[] tokens = srsString.split(":");
String authority;
String codeStr = tokens[tokens.length - 1];
int code = toNumericIdentifier(codeStr);
authority = DEFAULT_AUTHORITY;
if (tokens.length == 2) {
authority = tokens[0];
} else if( tokens.length == 7) {
authority = tokens[4];
}
return valueOf(authority, code);
}
private static int toNumericIdentifier(String codeStr) {
codeStr = codeStr.trim();
try {
return Integer.valueOf(codeStr);
}catch(NumberFormatException e){
throw new IllegalArgumentException(String.format("Can't parse value %s into an integer.", codeStr));
}
}
/**
* Returns an instance having the specified authority and code.
*
* @param authority the authority that assigned the code
* @param code the code for the CoordinateReferenceSystem
* @return a CrsId
for the specified authority and code.
*/
public static CrsId valueOf(String authority, int code) {
if (isUnknownEPSG(authority, code)) return CrsId.UNDEFINED;
return new CrsId(authority, code);
}
private static boolean isUnknownEPSG(String authority, int code) {
return DEFAULT_AUTHORITY.equalsIgnoreCase(authority) && (code <= 0);
}
/**
* Returns a CrsId
with the specified code and the
* EPSG as authority.
*
* @param code the code for the CrsId
.
* @return a CrsId
for the specified code,and EPSG as authority.
*/
public static CrsId valueOf(int code) {
return valueOf(CrsId.DEFAULT_AUTHORITY, code);
}
/**
* Creates an instance having the specified authority and code.
*
* If authority EPSG and 0 or -1 is passed for the code parameter, a value equal to CrsId.UNDEFINED
is returned.
*
* @param authority the authority that assigned the code
* @param code the code for the CoordinateReferenceSystem
*/
public CrsId(String authority, int code) {
if (authority == null || authority.isEmpty()) {
throw new IllegalArgumentException("Null or empty authority parameter.");
}
this.authority = authority.toUpperCase();
this.code = (code < 1 && DEFAULT_AUTHORITY.equals(this.authority)) ? -1 : code;
}
/**
* Returns the authority for this CrsId
.
*
* @return a String representing the authority for this CrsId
*/
public String getAuthority() {
return authority;
}
/**
* Returns the code for this CrsId
.
*
* @return the numeric code for this CrsId
*/
public int getCode() {
return code;
}
@Override
public String toString() {
return String.format("%S:%d", authority, code);
}
public String toUrn() {
return String.format("urn:ogc:def:crs:%s::%s", authority, code);
}
/**
* Creates an instance that identifies the crs identified by this {@code CrsId}, but
* extended with (optionally) a vertical or measure axis.
*
* @param zUnit the unit for the vertical axis (or null if there is no such axis)
* @param mUnit the unit for the measure axis (or null if there is no such axis)
* @return
*/
public CrsId extend(LinearUnit zUnit, Unit mUnit) {
return zUnit == null && mUnit == null ? this : new CrsExtendedId(this, zUnit, mUnit);
}
@Override
public boolean equals(Object o) {
if (this == o) return true;
if (o == null || getClass() != o.getClass()) return false;
CrsId crsId = (CrsId) o;
if (code != crsId.code) return false;
if (!authority.equals(crsId.authority)) return false;
return true;
}
@Override
public int hashCode() {
int result = authority.hashCode();
result = 31 * result + code;
return result;
}
}