public Transformation getTransformation(CoordinateSystem sourceCRS, CoordinateSystem targetCRS) { if (sourceCRS == null) { return null; } List<Element> toBeRemoved = new ArrayList<Element>(transformations.size()); List<String> sourceIDs = Arrays.asList(sourceCRS.getIdentifiers()); List<String> targetIDs = null; if (targetCRS != null) { targetIDs = Arrays.asList(targetCRS.getIdentifiers()); } else { targetIDs = new ArrayList<String>(); } Transformation result = null; for (int i = 0; i < transformations.size() && result == null; ++i) { Element transElem = transformations.get(i); if (transElem != null) { try { Element sourceCRSProp = XMLTools.getRequiredElement(transElem, PRE + "sourceCRS", nsContext); String transformSourceID = null; String transformTargetID = null; if (sourceCRSProp != null) { transformSourceID = sourceCRSProp.getAttributeNS(CommonNamespaces.XLNNS.toASCIIString(), "href"); if ("".equals(transformSourceID)) { transformSourceID = XMLTools.getRequiredNodeAsString( sourceCRSProp, "*[1]/" + PRE + "identifier", nsContext); } } if (targetCRS != null) { Element targetCRSProp = XMLTools.getRequiredElement(transElem, PRE + "targetCRS", nsContext); if (targetCRSProp != null) { transformTargetID = targetCRSProp.getAttributeNS(CommonNamespaces.XLNNS.toASCIIString(), "href"); if ("".equals(transformTargetID)) { transformTargetID = XMLTools.getRequiredNodeAsString( targetCRSProp, "*[1]/" + PRE + "identifier", nsContext); } } } if (sourceIDs.contains(transformSourceID)) { result = getProvider().parseTransformation(transElem); if (targetCRS == null) { // Trying to find a helmert transformation LOG.logDebug("Resolving a possible transformation."); if (result != null && !(result instanceof Helmert)) { result = getTransformation(result.getTargetCRS(), null); } } else { if (!targetIDs.contains(transformTargetID)) { LOG.logDebug( "Found a transformation with gml:id: " + transElem.getAttributeNS(CommonNamespaces.GML3_2_NS.toASCIIString(), "id") + ", but the target does not match the source crs, trying to build transformation chain."); Transformation second = getTransformation(result.getTargetCRS(), targetCRS); if (second != null) { result = new ConcatenatedTransform(result, second); } else { LOG.logDebug( "The transformation with gml:id: " + transElem.getAttributeNS( CommonNamespaces.GML3_2_NS.toASCIIString(), "id") + " is not the start of transformation chain, discarding it. "); result = null; } } } } } catch (XMLParsingException e) { toBeRemoved.add(transElem); LOG.logWarning( "No source CRS id could be found in this transformation(gml:id): " + transElem.getAttributeNS(CommonNamespaces.GML3_2_NS.toASCIIString(), "id") + " this is not correct, removing transformation from cache."); LOG.logWarning(e.getMessage()); } } if (toBeRemoved.size() > 0) { transformations.removeAll(toBeRemoved); } } return result; }
public CoordinateSystem getCRSByID(String id) throws CRSConfigurationException { if (resolver == null) { throw new CRSConfigurationException("No resolver initialized, this may not be."); } CoordinateSystem result = null; if (id != null && !"".equals(id.trim())) { LOG.logDebug("Trying to load crs with id: " + id + " from cache."); if (LOG.isDebug()) { LOG.logDebug(cachedIdentifiables.keySet().toString()); } if (cachedIdentifiables.containsKey(id)) { Identifiable r = cachedIdentifiables.get(id); LOG.logDebug("Found Identifiable: " + r.getIdAndName() + " from given id: " + id); if (!(r instanceof CoordinateSystem)) { LOG.logError( "Found Identifiable: " + r.getIdAndName() + " but it is not a coordinate system, your db is inconsistend return null."); r = null; } result = (CoordinateSystem) r; } if (result == null) { LOG.logDebug("No crs with id: " + id + " found in cache."); try { result = parseCoordinateSystem(resolver.getURIAsType(id)); } catch (IOException e) { LOG.logDebug(e.getLocalizedMessage(), e); throw new CRSConfigurationException(e); } if (result != null) { GeographicCRS t = null; if (result.getType() == CoordinateSystem.COMPOUND_CRS) { if (((CompoundCRS) result).getUnderlyingCRS().getType() == CoordinateSystem.PROJECTED_CRS) { t = ((ProjectedCRS) ((CompoundCRS) result).getUnderlyingCRS()).getGeographicCRS(); } else if (((CompoundCRS) result).getUnderlyingCRS().getType() == CoordinateSystem.GEOGRAPHIC_CRS) { t = (GeographicCRS) ((CompoundCRS) result).getUnderlyingCRS(); } else { LOG.logWarning( "Wgs84 Transformation lookup is currently only supported for GeographicCRS-chains."); } } else if (result.getType() == CoordinateSystem.PROJECTED_CRS) { t = ((ProjectedCRS) result).getGeographicCRS(); } else if (result.getType() == CoordinateSystem.GEOGRAPHIC_CRS) { t = (GeographicCRS) result; } else { LOG.logWarning( "Wgs84 Transformation lookup is currently only supported for GeographicCRS-chains."); } if (t != null) { Helmert wgs84 = t.getGeodeticDatum().getWGS84Conversion(); if (wgs84 == null) { wgs84 = resolver.getWGS84Transformation(t); } if (wgs84 != null) { if (wgs84.getSourceCRS() == null) { wgs84.setSourceCRS(t); addIdToCache(wgs84, true); } GeodeticDatum datum = result.getGeodeticDatum(); if (datum != null) { datum.setToWGS84(wgs84); // update the cache as well addIdToCache(datum, true); } } } } } } if (result == null) { LOG.logDebug( "The id: " + id + " could not be mapped to a valid deegree-crs, currently projectedCRS, geographicCRS, compoundCRS and geocentricCRS are supported."); } else { /** Adding the used underlying crs's to the cache. */ addIdToCache(result, false); if (result.getType() == CoordinateSystem.COMPOUND_CRS) { addIdToCache(((CompoundCRS) result).getUnderlyingCRS(), false); if (((CompoundCRS) result).getUnderlyingCRS().getType() == CoordinateSystem.PROJECTED_CRS) { addIdToCache( ((ProjectedCRS) ((CompoundCRS) result).getUnderlyingCRS()).getGeographicCRS(), false); } } else if (result.getType() == CoordinateSystem.PROJECTED_CRS) { addIdToCache(((ProjectedCRS) result).getGeographicCRS(), false); } } return result; }
public Helmert getWGS84Transformation(GeographicCRS sourceCRS) { if (sourceCRS == null) { return null; } if (LOG.isDebug()) { LOG.logDebug( "Searching for wgs84 transformation for given sourceCRS: " + Arrays.toString(sourceCRS.getIdentifiers())); } Helmert result = cachedWGS84Transformations.get(sourceCRS); if (result == null) { Transformation parsedTransformation = getTransformation(sourceCRS, null); if (parsedTransformation instanceof Helmert) { LOG.logDebug( "Found an helmert transformation for sourceCRS: " + Arrays.toString(sourceCRS.getIdentifiers())); result = (Helmert) parsedTransformation; } else { if (parsedTransformation instanceof CRSTransformation) { CoordinateSystem target = ((CRSTransformation) parsedTransformation).getTargetCRS(); GeographicCRS t = null; if (LOG.isDebug()) { LOG.logDebug( "Found crstransformation for sourceCRS: " + Arrays.toString(sourceCRS.getIdentifiers()) + " and targetCRS: " + Arrays.toString(target.getIdentifiers()) + " will now use the targetCRS to find a Helmert transformation."); } if (target.getType() == CoordinateSystem.COMPOUND_CRS) { if (((CompoundCRS) target).getUnderlyingCRS().getType() == CoordinateSystem.PROJECTED_CRS) { t = ((ProjectedCRS) ((CompoundCRS) target).getUnderlyingCRS()).getGeographicCRS(); } else if (((CompoundCRS) target).getUnderlyingCRS().getType() == CoordinateSystem.GEOGRAPHIC_CRS) { t = (GeographicCRS) target; } else { LOG.logWarning( "Wgs84 Transformation lookup is currently only supported for GeographicCRS-chains."); } } else if (target.getType() == CoordinateSystem.PROJECTED_CRS) { t = ((ProjectedCRS) target).getGeographicCRS(); } else if (target.getType() == CoordinateSystem.GEOGRAPHIC_CRS) { t = (GeographicCRS) target; } else { LOG.logWarning( "Wgs84 Transformation lookup is currently only supported for GeographicCRS-chains."); } if (t != null) { if (LOG.isDebug()) { LOG.logDebug( "Trying to resolve target to find a wgs84transformation for the 'targetCRS': " + Arrays.toString(t.getIdentifiers())); } result = getWGS84Transformation(t); } } else { LOG.logWarning( "The transformation is not an instance of CRSTransformation nor a Helmert, ignoring it."); } } } if (result != null) { if (LOG.isDebug()) { LOG.logDebug( "For the given crs: " + sourceCRS.getIdentifier() + " following helmert transformation was found:\n" + result); } cachedWGS84Transformations.put(sourceCRS, result); } else { LOG.logInfo( "No helmert transformation found for the given crs: " + sourceCRS.getIdentifier()); } return result; }