Search in sources :

Example 6 with Hints

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;
}
Also used : Geometry(org.locationtech.jts.geom.Geometry) ReferenceIdentifier(org.opengis.referencing.ReferenceIdentifier) Hints(org.geotools.factory.Hints) MathTransform(org.opengis.referencing.operation.MathTransform) FactoryException(org.opengis.referencing.FactoryException) GeoFormatException(org.codice.ddf.libs.geo.GeoFormatException) TransformException(org.opengis.referencing.operation.TransformException) CRSAuthorityFactory(org.opengis.referencing.crs.CRSAuthorityFactory) CoordinateReferenceSystem(org.opengis.referencing.crs.CoordinateReferenceSystem)

Example 7 with Hints

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);
}
Also used : Query(org.geotools.data.Query) Hints(org.geotools.factory.Hints) CoordinateSequenceFactory(com.vividsolutions.jts.geom.CoordinateSequenceFactory) PackedCoordinateSequenceFactory(com.vividsolutions.jts.geom.impl.PackedCoordinateSequenceFactory) PackedCoordinateSequenceFactory(com.vividsolutions.jts.geom.impl.PackedCoordinateSequenceFactory)

Example 8 with Hints

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();
    }
}
Also used : NoSuchAuthorityCodeException(org.opengis.referencing.NoSuchAuthorityCodeException) Set(java.util.Set) Hints(org.geotools.factory.Hints) FactoryException(org.opengis.referencing.FactoryException) ValueComboBoxData(com.sldeditor.ui.widgets.ValueComboBoxData) Identifier(org.opengis.metadata.Identifier) ReferenceIdentifier(org.opengis.referencing.ReferenceIdentifier) VendorOptionVersion(com.sldeditor.common.vendoroption.VendorOptionVersion) Collection(java.util.Collection) AuthorityFactory(org.opengis.referencing.AuthorityFactory) Citation(org.opengis.metadata.citation.Citation)

Example 9 with Hints

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();
    }
}
Also used : Hints(org.geotools.factory.Hints) Query(org.geotools.data.Query) HashMap(java.util.HashMap) ArrayList(java.util.ArrayList) StreetEdge(org.opentripplanner.routing.edgetype.StreetEdge) LineString(com.vividsolutions.jts.geom.LineString) NonLocalizedString(org.opentripplanner.util.NonLocalizedString) MultiLineString(com.vividsolutions.jts.geom.MultiLineString) TreeSet(java.util.TreeSet) StreetTraversalPermission(org.opentripplanner.routing.edgetype.StreetTraversalPermission) CoordinateReferenceSystem(org.opengis.referencing.crs.CoordinateReferenceSystem) HashSet(java.util.HashSet) P2(org.opentripplanner.common.model.P2) CRSAuthorityFactory(org.opengis.referencing.crs.CRSAuthorityFactory) SimpleFeature(org.opengis.feature.simple.SimpleFeature) SimpleFeatureType(org.opengis.feature.simple.SimpleFeatureType) Coordinate(com.vividsolutions.jts.geom.Coordinate) LineString(com.vividsolutions.jts.geom.LineString) MultiLineString(com.vividsolutions.jts.geom.MultiLineString) NonLocalizedString(org.opentripplanner.util.NonLocalizedString) IntersectionVertex(org.opentripplanner.routing.vertextype.IntersectionVertex) Alert(org.opentripplanner.routing.alertpatch.Alert)

Example 10 with Hints

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);
        }
    }
}
Also used : NoSuchAuthorityCodeException(org.opengis.referencing.NoSuchAuthorityCodeException) Hints(org.geotools.factory.Hints) FactoryException(org.opengis.referencing.FactoryException) ValueComboBoxData(com.sldeditor.ui.widgets.ValueComboBoxData) Identifier(org.opengis.metadata.Identifier) ReferenceIdentifier(org.opengis.referencing.ReferenceIdentifier) VendorOptionVersion(com.sldeditor.common.vendoroption.VendorOptionVersion) Collection(java.util.Collection) AuthorityFactory(org.opengis.referencing.AuthorityFactory) Citation(org.opengis.metadata.citation.Citation) CoordinateReferenceSystem(org.opengis.referencing.crs.CoordinateReferenceSystem)

Aggregations

Hints (org.geotools.factory.Hints)10 CoordinateReferenceSystem (org.opengis.referencing.crs.CoordinateReferenceSystem)5 FactoryException (org.opengis.referencing.FactoryException)4 CRSAuthorityFactory (org.opengis.referencing.crs.CRSAuthorityFactory)4 ReferenceIdentifier (org.opengis.referencing.ReferenceIdentifier)3 VendorOptionVersion (com.sldeditor.common.vendoroption.VendorOptionVersion)2 ValueComboBoxData (com.sldeditor.ui.widgets.ValueComboBoxData)2 Collection (java.util.Collection)2 GeoFormatException (org.codice.ddf.libs.geo.GeoFormatException)2 Query (org.geotools.data.Query)2 WKTParser (org.geotools.geometry.text.WKTParser)2 SimpleFeature (org.opengis.feature.simple.SimpleFeature)2 SimpleFeatureType (org.opengis.feature.simple.SimpleFeatureType)2 PositionFactory (org.opengis.geometry.PositionFactory)2 AggregateFactory (org.opengis.geometry.aggregate.AggregateFactory)2 GeometryFactory (org.opengis.geometry.coordinate.GeometryFactory)2 PrimitiveFactory (org.opengis.geometry.primitive.PrimitiveFactory)2 Identifier (org.opengis.metadata.Identifier)2 Citation (org.opengis.metadata.citation.Citation)2 AuthorityFactory (org.opengis.referencing.AuthorityFactory)2