use of org.apache.sis.internal.util.StandardDateFormat in project sis by apache.
the class EPSGDataAccess method createDatum.
/**
* Creates an arbitrary datum from a code. The returned object will typically be an
* instance of {@link GeodeticDatum}, {@link VerticalDatum} or {@link TemporalDatum}.
*
* <div class="note"><b>Example:</b>
* some EPSG codes for datums are:
*
* <table class="sis" summary="EPSG codes examples">
* <tr><th>Code</th> <th>Type</th> <th>Description</th></tr>
* <tr><td>6326</td> <td>Geodetic</td> <td>World Geodetic System 1984</td></tr>
* <tr><td>6322</td> <td>Geodetic</td> <td>World Geodetic System 1972</td></tr>
* <tr><td>1027</td> <td>Vertical</td> <td>EGM2008 geoid</td></tr>
* <tr><td>5100</td> <td>Vertical</td> <td>Mean Sea Level</td></tr>
* <tr><td>9315</td> <td>Engineering</td> <td>Seismic bin grid datum</td></tr>
* </table></div>
*
* @param code value allocated by EPSG.
* @return the datum for the given code.
* @throws NoSuchAuthorityCodeException if the specified {@code code} was not found.
* @throws FactoryException if the object creation failed for some other reason.
*/
@Override
public synchronized Datum createDatum(final String code) throws NoSuchAuthorityCodeException, FactoryException {
ArgumentChecks.ensureNonNull("code", code);
Datum returnValue = null;
try (ResultSet result = executeQuery("Datum", "DATUM_CODE", "DATUM_NAME", "SELECT DATUM_CODE," + " DATUM_NAME," + " DATUM_TYPE," + " ORIGIN_DESCRIPTION," + " REALIZATION_EPOCH," + " AREA_OF_USE_CODE," + " DATUM_SCOPE," + " REMARKS," + " DEPRECATED," + // Only for geodetic type
" ELLIPSOID_CODE," + // Only for geodetic type
" PRIME_MERIDIAN_CODE" + " FROM [Datum]" + " WHERE DATUM_CODE = ?", code)) {
while (result.next()) {
final Integer epsg = getInteger(code, result, 1);
final String name = getString(code, result, 2);
final String type = getString(code, result, 3);
final String anchor = getOptionalString(result, 4);
final String epoch = getOptionalString(result, 5);
final String area = getOptionalString(result, 6);
final String scope = getOptionalString(result, 7);
final String remarks = getOptionalString(result, 8);
final boolean deprecated = getOptionalBoolean(result, 9);
Map<String, Object> properties = createProperties("Datum", name, epsg, area, scope, remarks, deprecated);
if (anchor != null) {
properties.put(Datum.ANCHOR_POINT_KEY, anchor);
}
if (epoch != null)
try {
/*
* Parse the date manually because it is declared as a VARCHAR instead than DATE in original
* SQL scripts. Apache SIS installer replaces VARCHAR by DATE, but we have no guarantee that
* we are reading an EPSG database created by our installer. Furthermore an older version of
* EPSG installer was using SMALLINT instead than DATE, because scripts before EPSG 9.0 were
* reporting only the epoch year.
*/
final CharSequence[] fields = CharSequences.split(epoch, '-');
int year = 0, month = 0, day = 1;
for (int i = Math.min(fields.length, 3); --i >= 0; ) {
final int f = Integer.parseInt(fields[i].toString());
switch(i) {
case 0:
year = f;
break;
case 1:
month = f - 1;
break;
case 2:
day = f;
break;
}
}
if (year != 0) {
final Calendar calendar = getCalendar();
calendar.set(year, month, day);
properties.put(Datum.REALIZATION_EPOCH_KEY, calendar.getTime());
}
} catch (NumberFormatException exception) {
// Not a fatal error.
unexpectedException("createDatum", exception);
}
/*
* The following switch statement should have a case for all "epsg_datum_kind" values enumerated
* in the "EPSG_Prepare.sql" file, except that the values in this Java code are in lower cases.
*/
final DatumFactory datumFactory = owner.datumFactory;
final Datum datum;
switch(type.toLowerCase(Locale.US)) {
/*
* The "geodetic" case invokes createProperties(…) indirectly through calls to
* createEllipsoid(String) and createPrimeMeridian(String), so we must protect
* the properties map from changes.
*/
case "geodetic":
{
// Protect from changes
properties = new HashMap<>(properties);
final Ellipsoid ellipsoid = owner.createEllipsoid(getString(code, result, 10));
final PrimeMeridian meridian = owner.createPrimeMeridian(getString(code, result, 11));
final BursaWolfParameters[] param = createBursaWolfParameters(meridian, epsg);
if (param != null) {
properties.put(DefaultGeodeticDatum.BURSA_WOLF_KEY, param);
}
datum = datumFactory.createGeodeticDatum(properties, ellipsoid, meridian);
break;
}
/*
* Vertical datum type is hard-coded to geoidal. It would be possible to infer other
* types by looking at the coordinate system, but it could result in different datum
* associated to the same EPSG code. Since vertical datum type is no longer part of
* ISO 19111:2007, it is probably not worth to handle such cases.
*/
case "vertical":
{
datum = datumFactory.createVerticalDatum(properties, VerticalDatumType.GEOIDAL);
break;
}
/*
* Origin date is stored in ORIGIN_DESCRIPTION field. A column of SQL type
* "date" type would have been better, but we do not modify the EPSG model.
*/
case "temporal":
{
final Date originDate;
if (anchor == null || anchor.isEmpty()) {
throw new FactoryDataException(resources().getString(Resources.Keys.DatumOriginShallBeDate));
}
if (dateFormat == null) {
dateFormat = new StandardDateFormat();
// Use UTC timezone.
dateFormat.setCalendar(getCalendar());
}
try {
originDate = dateFormat.parse(anchor);
} catch (ParseException e) {
throw new FactoryDataException(resources().getString(Resources.Keys.DatumOriginShallBeDate), e);
}
datum = datumFactory.createTemporalDatum(properties, originDate);
break;
}
/*
* Straightforward case.
*/
case "engineering":
{
datum = datumFactory.createEngineeringDatum(properties);
break;
}
case "parametric":
{
datum = ReferencingServices.getInstance().createParametricDatum(properties, datumFactory);
break;
}
default:
{
throw new FactoryDataException(error().getString(Errors.Keys.UnknownType_1, type));
}
}
returnValue = ensureSingleton(datum, returnValue, code);
if (result.isClosed()) {
// Because of the recursive call done by createBursaWolfParameters(…).
break;
}
}
} catch (SQLException exception) {
throw databaseFailure(Datum.class, code, exception);
}
if (returnValue == null) {
throw noSuchAuthorityCode(Datum.class, code);
}
return returnValue;
}
Aggregations