use of org.geotools.factory.Hints in project ddf by codice.
the class GeospatialUtil method transformToEPSG4326LonLatFormat.
/**
* Transform a geometry to EPSG:4326 format with lon/lat coordinate ordering. NOTE: This method
* will NOT perform the transform swapping coordinates even if the sourceCrsName is EPSG:4326.
*
* @param geometry - Geometry to transform
* @param sourceCrs - Source geometry's coordinate reference system
* @return Geometry - Transformed geometry into EPSG:4326 lon/lat coordinate system
*/
public static Geometry transformToEPSG4326LonLatFormat(Geometry geometry, CoordinateReferenceSystem sourceCrs) throws GeoFormatException {
if (geometry == null) {
throw new GeoFormatException("Unable to convert null geometry");
}
// information
if (sourceCrs == null || CollectionUtils.isEmpty(sourceCrs.getIdentifiers())) {
return geometry;
}
Geometry transformedGeometry = geometry;
try {
boolean sourceCrsMatchesTarget = false;
for (ReferenceIdentifier referenceIdentifier : sourceCrs.getIdentifiers()) {
if (referenceIdentifier.toString().equalsIgnoreCase(EPSG_4326)) {
sourceCrsMatchesTarget = true;
break;
}
}
if (!sourceCrsMatchesTarget) {
Hints hints = new Hints(Hints.FORCE_LONGITUDE_FIRST_AXIS_ORDER, Boolean.TRUE);
CRSAuthorityFactory factory = ReferencingFactoryFinder.getCRSAuthorityFactory("EPSG", hints);
CoordinateReferenceSystem targetCRS = factory.createCoordinateReferenceSystem(EPSG_4326);
MathTransform transform = CRS.findMathTransform(sourceCrs, targetCRS);
transformedGeometry = JTS.transform(geometry, transform);
LOGGER.debug("Converted CRS {} into {} : {}", sourceCrs, EPSG_4326, geometry);
}
} catch (FactoryException | TransformException e) {
throw new GeoFormatException("Unable to convert coordinate to " + EPSG_4326, e);
}
return transformedGeometry;
}
use of org.geotools.factory.Hints in project GeoGig by boundlessgeo.
the class ImportOp method insert.
private void insert(final WorkingTree workTree, final String path, @SuppressWarnings("rawtypes") final FeatureSource featureSource, final ProgressListener taskProgress) {
final Query query = new Query();
CoordinateSequenceFactory coordSeq = new PackedCoordinateSequenceFactory();
query.getHints().add(new Hints(Hints.JTS_COORDINATE_SEQUENCE_FACTORY, coordSeq));
workTree.insert(path, featureSource, query, taskProgress);
}
use of org.geotools.factory.Hints 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.geotools.factory.Hints in project OpenTripPlanner by opentripplanner.
the class ShapefileStreetModule method buildGraph.
@Override
public void buildGraph(Graph graph, HashMap<Class<?>, Object> extra) {
try {
FeatureSource<SimpleFeatureType, SimpleFeature> featureSource = _featureSourceFactory.getFeatureSource();
CoordinateReferenceSystem sourceCRS = featureSource.getInfo().getCRS();
Hints hints = new Hints(Hints.FORCE_LONGITUDE_FIRST_AXIS_ORDER, Boolean.TRUE);
CRSAuthorityFactory factory = ReferencingFactoryFinder.getCRSAuthorityFactory("EPSG", hints);
CoordinateReferenceSystem worldCRS = factory.createCoordinateReferenceSystem("EPSG:4326");
Query query = new Query();
query.setCoordinateSystem(sourceCRS);
query.setCoordinateSystemReproject(worldCRS);
FeatureCollection<SimpleFeatureType, SimpleFeature> features = featureSource.getFeatures(query);
features = featureSource.getFeatures(query);
HashMap<String, HashMap<Coordinate, Integer>> intersectionNameToId = new HashMap<String, HashMap<Coordinate, Integer>>();
SimpleFeatureConverter<String> streetIdConverter = _schema.getIdConverter();
SimpleFeatureConverter<String> streetNameConverter = _schema.getNameConverter();
SimpleFeatureConverter<P2<StreetTraversalPermission>> permissionConverter = _schema.getPermissionConverter();
SimpleFeatureConverter<String> noteConverter = _schema.getNoteConverter();
HashMap<Coordinate, IntersectionVertex> intersectionsByLocation = new HashMap<Coordinate, IntersectionVertex>();
SimpleFeatureConverter<P2<Double>> safetyConverter = _schema.getBicycleSafetyConverter();
SimpleFeatureConverter<Boolean> slopeOverrideCoverter = _schema.getSlopeOverrideConverter();
SimpleFeatureConverter<Boolean> featureSelector = _schema.getFeatureSelector();
// Keep track of features that are duplicated so we don't have duplicate streets
Set<Object> seen = new HashSet<Object>();
List<SimpleFeature> featureList = new ArrayList<SimpleFeature>();
FeatureIterator<SimpleFeature> it2 = features.features();
while (it2.hasNext()) {
SimpleFeature feature = it2.next();
if (featureSelector != null && !featureSelector.convert(feature)) {
continue;
}
featureList.add(feature);
}
it2.close();
it2 = null;
HashMap<Coordinate, TreeSet<String>> coordinateToStreetNames = getCoordinatesToStreetNames(featureList);
for (SimpleFeature feature : featureList) {
if (feature.getDefaultGeometry() == null) {
log.warn("feature has no geometry: " + feature.getIdentifier());
continue;
}
LineString geom = toLineString((Geometry) feature.getDefaultGeometry());
Object o = streetIdConverter.convert(feature);
String label = "" + o;
if (o != null && seen.contains(label)) {
continue;
}
seen.add(label);
String name = streetNameConverter.convert(feature);
Coordinate[] coordinates = geom.getCoordinates();
if (coordinates.length < 2) {
// not a real linestring
log.warn("Bad geometry for street with label " + label + " name " + name);
continue;
}
// this rounding is a total hack, to work around
// http://jira.codehaus.org/browse/GEOT-2811
Coordinate startCoordinate = new Coordinate(Math.round(coordinates[0].x * 1048576) / 1048576.0, Math.round(coordinates[0].y * 1048576) / 1048576.0);
Coordinate endCoordinate = new Coordinate(Math.round(coordinates[coordinates.length - 1].x * 1048576) / 1048576.0, Math.round(coordinates[coordinates.length - 1].y * 1048576) / 1048576.0);
String startIntersectionName = getIntersectionName(coordinateToStreetNames, intersectionNameToId, startCoordinate);
if (startIntersectionName == "null") {
log.warn("No intersection name for " + name);
}
String endIntersectionName = getIntersectionName(coordinateToStreetNames, intersectionNameToId, endCoordinate);
IntersectionVertex startIntersection = intersectionsByLocation.get(startCoordinate);
if (startIntersection == null) {
startIntersection = new IntersectionVertex(graph, startIntersectionName, startCoordinate.x, startCoordinate.y, new NonLocalizedString(startIntersectionName));
intersectionsByLocation.put(startCoordinate, startIntersection);
}
IntersectionVertex endIntersection = intersectionsByLocation.get(endCoordinate);
if (endIntersection == null) {
endIntersection = new IntersectionVertex(graph, endIntersectionName, endCoordinate.x, endCoordinate.y, new NonLocalizedString(endIntersectionName));
intersectionsByLocation.put(endCoordinate, endIntersection);
}
double length = 0;
for (int i = 0; i < coordinates.length - 1; ++i) {
length += JTS.orthodromicDistance(coordinates[i], coordinates[i + 1], worldCRS);
}
P2<StreetTraversalPermission> permissions = permissionConverter.convert(feature);
// TODO Set appropriate car speed from shapefile source.
StreetEdge street = edgeFactory.createEdge(startIntersection, endIntersection, geom, new NonLocalizedString(name), length, permissions.first, false);
LineString reversed = (LineString) geom.reverse();
StreetEdge backStreet = edgeFactory.createEdge(endIntersection, startIntersection, reversed, new NonLocalizedString(name), length, permissions.second, true);
backStreet.shareData(street);
if (noteConverter != null) {
String note = noteConverter.convert(feature);
if (note != null && note.length() > 0) {
Alert noteAlert = Alert.createSimpleAlerts(note);
graph.streetNotesService.addStaticNote(street, noteAlert, StreetNotesService.ALWAYS_MATCHER);
graph.streetNotesService.addStaticNote(backStreet, noteAlert, StreetNotesService.ALWAYS_MATCHER);
}
}
boolean slopeOverride = slopeOverrideCoverter.convert(feature);
street.setSlopeOverride(slopeOverride);
backStreet.setSlopeOverride(slopeOverride);
if (safetyConverter != null) {
P2<Double> safetyFactors = safetyConverter.convert(feature);
if (safetyFactors != null) {
street.setBicycleSafetyFactor(safetyFactors.first.floatValue());
backStreet.setBicycleSafetyFactor(safetyFactors.second.floatValue());
}
}
}
} catch (Exception ex) {
throw new IllegalStateException("error loading shapefile street data", ex);
} finally {
_featureSourceFactory.cleanup();
}
}
use of org.geotools.factory.Hints in project sldeditor by robward-scisys.
the class CoordManager method processCRSEntry.
/**
* Process CRS entry.
*/
private void processCRSEntry() {
VendorOptionVersion vendorOptionVersion = VendorOptionManager.getInstance().getDefaultVendorOptionVersion();
ValueComboBoxData notSetValue = new ValueComboBoxData(NOT_SET_CRS, Localisation.getString(CoordManager.class, Localisation.COMMON_NOT_SET), 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) {
// Do nothing
} catch (FactoryException e) {
ConsoleManager.getInstance().exception(this, e);
}
}
}
Aggregations