use of org.opengis.referencing.NoSuchAuthorityCodeException in project GeoGig by boundlessgeo.
the class MappingRule method getFeatureType.
/**
* Returns the feature type defined by this rule. This is the feature type that features
* transformed by this rule will have
*
* @return
*/
public SimpleFeatureType getFeatureType() {
if (featureType == null) {
SimpleFeatureTypeBuilder fb = new SimpleFeatureTypeBuilder();
fb.setName(name);
fb.add("id", Long.class);
if (defaultFields != null) {
for (DefaultField df : defaultFields) {
fb.add(df.name().toLowerCase(), df.getFieldClass());
}
}
Set<String> keys = this.fields.keySet();
for (String key : keys) {
AttributeDefinition field = fields.get(key);
Class<?> clazz = field.getType().getBinding();
if (Geometry.class.isAssignableFrom(clazz)) {
Preconditions.checkArgument(geometryType == null, "The mapping has more than one geometry attribute");
CoordinateReferenceSystem epsg4326;
try {
epsg4326 = CRS.decode("EPSG:4326", true);
fb.add(field.getName(), clazz, epsg4326);
} catch (NoSuchAuthorityCodeException e) {
} catch (FactoryException e) {
}
geometryType = clazz;
} else {
fb.add(field.getName(), clazz);
}
}
Preconditions.checkNotNull(geometryType, "The mapping rule does not define a geometry field");
if (!geometryType.equals(Point.class)) {
fb.add("nodes", String.class);
}
featureType = fb.buildFeatureType();
featureBuilder = new SimpleFeatureBuilder(featureType);
}
return featureType;
}
use of org.opengis.referencing.NoSuchAuthorityCodeException in project sldeditor by robward-scisys.
the class CoordManager method populateCRSList.
/**
* Populate crs list.
*/
public void populateCRSList() {
if (isPopulated()) {
Runnable runnable = () -> {
VendorOptionVersion vendorOptionVersion = VendorOptionManager.getInstance().getDefaultVendorOptionVersion();
ValueComboBoxData notSetValue = new ValueComboBoxData(NOT_SET_CRS, Localisation.getString(CoordManager.class, "common.notSet"), vendorOptionVersion);
crsDataList.add(notSetValue);
Hints hints = null;
for (AuthorityFactory factory : ReferencingFactoryFinder.getCRSAuthorityFactories(hints)) {
String authorityCode = NOT_SET_CRS;
Citation citation = factory.getAuthority();
if (citation != null) {
@SuppressWarnings("unchecked") Collection<Identifier> identifierList = (Collection<Identifier>) citation.getIdentifiers();
authorityCode = identifierList.iterator().next().getCode();
}
Set<String> codeList;
try {
codeList = factory.getAuthorityCodes(CoordinateReferenceSystem.class);
for (String code : codeList) {
String fullCode = String.format("%s:%s", authorityCode, code);
String descriptionText = factory.getDescriptionText(code).toString();
String text = String.format("%s - %s", fullCode, descriptionText);
ValueComboBoxData value = new ValueComboBoxData(fullCode, text, vendorOptionVersion);
crsDataList.add(value);
crsMap.put(fullCode, value);
}
} catch (NoSuchAuthorityCodeException e) {
// ConsoleManager.getInstance().exception(this, e);
} catch (FactoryException e) {
ConsoleManager.getInstance().exception(this, e);
}
}
};
Thread thread = new Thread(runnable);
thread.start();
}
}
use of org.opengis.referencing.NoSuchAuthorityCodeException in project sis by apache.
the class EPSGFactoryFallback method predefined.
/**
* Implementation of all {@code createFoo(String)} methods in this fallback class.
*
* @param code the EPSG code.
* @param kind any combination of {@link #CRS}, {@link #DATUM}, {@link #ELLIPSOID} or {@link #PRIME_MERIDIAN} bits.
* @return the requested object.
* @throws NoSuchAuthorityCodeException if no matching object has been found.
*/
private IdentifiedObject predefined(String code, final int kind) throws NoSuchAuthorityCodeException {
try {
/*
* Parse the value after the last ':'. We do not bother to verify if the part before ':' is legal
* (e.g. "EPSG:4326", "EPSG::4326", "urn:ogc:def:crs:epsg::4326", etc.) because this analysis has
* already be done by MultiAuthoritiesFactory. We nevertheless skip the prefix in case this factory
* is used directly (not through MultiAuthoritiesFactory), which should be rare. The main case is
* when using the factory returned by AuthorityFactories.fallback(…).
*/
code = CharSequences.trimWhitespaces(code, code.lastIndexOf(DefaultNameSpace.DEFAULT_SEPARATOR) + 1, code.length()).toString();
final int n = Integer.parseInt(code);
if ((kind & PRIME_MERIDIAN) != 0 && n == Constants.EPSG_GREENWICH) {
return CommonCRS.WGS84.primeMeridian();
}
for (final CommonCRS crs : CommonCRS.values()) {
/*
* In a complete EPSG dataset we could have an ambiguity below because the same code can be used
* for datum, ellipsoid and CRS objects. However in the particular case of this EPSG-subset, we
* ensured that there is no such collision - see CommonCRSTest.ensureNoCodeCollision().
*/
if ((kind & ELLIPSOID) != 0 && n == crs.ellipsoid)
return crs.ellipsoid();
if ((kind & DATUM) != 0 && n == crs.datum)
return crs.datum();
if ((kind & CRS) != 0) {
if (n == crs.geographic)
return crs.geographic();
if (n == crs.geocentric)
return crs.geocentric();
if (n == crs.geo3D)
return crs.geographic3D();
final double latitude;
int zone;
if (crs.northUTM != 0 && (zone = n - crs.northUTM) >= crs.firstZone && zone <= crs.lastZone) {
// Any north latitude below 56°N (because of Norway exception) is okay
latitude = +1;
} else if (crs.southUTM != 0 && (zone = n - crs.southUTM) >= crs.firstZone && zone <= crs.lastZone) {
// Any south latitude above 80°S (because of UPS south case) is okay.
latitude = -1;
} else if (n == crs.northUPS) {
latitude = Latitude.MAX_VALUE;
// Any random UTM zone is okay.
zone = 30;
} else if (n == crs.southUPS) {
latitude = Latitude.MIN_VALUE;
// Any random UTM zone is okay.
zone = 30;
} else {
continue;
}
return crs.universal(latitude, TransverseMercator.Zoner.UTM.centralMeridian(zone));
}
}
if ((kind & (DATUM | CRS)) != 0) {
for (final CommonCRS.Vertical candidate : CommonCRS.Vertical.values()) {
if (candidate.isEPSG) {
if ((kind & DATUM) != 0 && candidate.datum == n)
return candidate.datum();
if ((kind & CRS) != 0 && candidate.crs == n)
return candidate.crs();
}
}
}
} catch (NumberFormatException cause) {
final NoSuchAuthorityCodeException e = new NoSuchAuthorityCodeException(Resources.format(Resources.Keys.NoSuchAuthorityCode_3, Constants.EPSG, toClass(kind), code), AUTHORITY, code);
e.initCause(cause);
throw e;
}
throw new NoSuchAuthorityCodeException(Resources.format(Resources.Keys.NoSuchAuthorityCodeInSubset_4, Constants.EPSG, toClass(kind), code, "http://sis.apache.org/epsg.html"), AUTHORITY, code);
}
use of org.opengis.referencing.NoSuchAuthorityCodeException in project sis by apache.
the class IdentifierCommand method create.
/**
* Creates an identifier row for the given CRS.
* This method gives precedence to {@code "urn:ogc:def:"} identifiers if possible.
*
* @return the row, or {@code null} if no identifier has been found.
*/
static Row create(ReferenceSystem rs) throws FactoryException {
String identifier = IdentifiedObjects.lookupURN(rs, null);
if (identifier == null) {
/*
* If we can not find an identifier matching the EPSG or WMS definitions,
* look at the identifiers declared in the CRS and verify their validity.
*/
for (final Identifier id : rs.getIdentifiers()) {
final String c = IdentifiedObjects.toURN(rs.getClass(), id);
if (c != null) {
identifier = c;
// Stop at the first "urn:ogc:def:…".
break;
}
if (identifier == null) {
// "AUTHORITY:CODE" as a fallback if no URN.
identifier = IdentifiedObjects.toString(id);
}
}
if (identifier == null) {
// No identifier found.
return null;
}
}
/*
* The CRS provided by the user contains identifier, but the 'lookupURN' operation above failed to
* find it. The most likely cause is that the user-provided CRS does not use the same axis order.
*/
State state;
try {
final ReferenceSystem def = CRS.forCode(identifier);
final ComparisonMode c = ComparisonMode.equalityLevel(def, rs);
if (c == null) {
state = State.MISMATCH;
} else
switch(c) {
case ALLOW_VARIANT:
{
state = State.AXIS_ORDER;
break;
}
case APPROXIMATIVE:
{
state = State.APPROXIMATIVE;
rs = def;
break;
}
default:
{
state = State.VALID;
rs = def;
break;
}
}
} catch (NoSuchAuthorityCodeException e) {
state = State.UNKNOWN;
}
return new Row(state, identifier, rs.getName().getCode());
}
use of org.opengis.referencing.NoSuchAuthorityCodeException in project sis by apache.
the class EPSGDataAccess method toPrimaryKeys.
/**
* Converts EPSG codes or EPSG names to the numerical identifiers (the primary keys).
*
* <div class="note"><b>Note:</b>
* this method could be seen as the converse of above {@link #getDescriptionText(String)} method.</div>
*
* @param table the table where the code should appears, or {@code null} if none.
* @param codeColumn the column name for the codes, or {@code null} if none.
* @param nameColumn the column name for the names, or {@code null} if none.
* @param codes the codes or names to convert to primary keys, as an array of length 1 or 2.
* @return the numerical identifiers (i.e. the table primary key values).
* @throws SQLException if an error occurred while querying the database.
*/
private int[] toPrimaryKeys(final String table, final String codeColumn, final String nameColumn, final String... codes) throws SQLException, FactoryException {
final int[] primaryKeys = new int[codes.length];
for (int i = 0; i < codes.length; i++) {
final String code = codes[i];
if (codeColumn != null && nameColumn != null && !isPrimaryKey(code)) {
/*
* The given string is not a numerical code. Search the value in the database.
* If a prepared statement is already available, reuse it providing that it was
* created for the current table. Otherwise we will create a new statement.
*/
final String KEY = "PrimaryKey";
PreparedStatement statement = statements.get(KEY);
if (statement != null) {
if (!table.equals(lastTableForName)) {
statements.remove(KEY);
statement.close();
statement = null;
lastTableForName = null;
}
}
if (statement == null) {
statement = connection.prepareStatement(translator.apply("SELECT " + codeColumn + ", " + nameColumn + " FROM [" + table + "] WHERE " + nameColumn + " LIKE ?"));
statements.put(KEY, statement);
lastTableForName = table;
}
statement.setString(1, toLikePattern(code));
Integer resolved = null;
try (ResultSet result = statement.executeQuery()) {
while (result.next()) {
if (SQLUtilities.filterFalsePositive(code, result.getString(2))) {
resolved = ensureSingleton(getOptionalInteger(result, 1), resolved, code);
}
}
}
if (resolved != null) {
primaryKeys[i] = resolved;
continue;
}
}
/*
* At this point, 'identifier' should be the primary key. It may still be a non-numerical string
* if we the above code did not found a match in the name column.
*/
try {
primaryKeys[i] = Integer.parseInt(code);
} catch (NumberFormatException e) {
throw (NoSuchAuthorityCodeException) new NoSuchAuthorityCodeException(error().getString(Errors.Keys.IllegalIdentifierForCodespace_2, Constants.EPSG, code), Constants.EPSG, code).initCause(e);
}
}
return primaryKeys;
}
Aggregations