Java Code Examples for org.opengis.feature.simple.SimpleFeatureType#getCoordinateReferenceSystem()

The following examples show how to use org.opengis.feature.simple.SimpleFeatureType#getCoordinateReferenceSystem() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: CreateExternalDataSource.java    From sldeditor with GNU General Public License v3.0 6 votes vote down vote up
/**
 * Connect to vector data source.
 *
 * @param typeName the type name
 * @param dataStore the data store
 * @throws IOException Signals that an I/O exception has occurred.
 */
private void connectToVectorDataSource(String typeName, DataStore dataStore)
        throws IOException {
    dsInfo.setTypeName(typeName);

    SimpleFeatureSource source = dataStore.getFeatureSource(typeName);
    SimpleFeatureType schema = source.getSchema();

    if (schema.getCoordinateReferenceSystem() == null) {
        // No crs found to set a default and reload
        if (dataStore instanceof ShapefileDataStore) {
            ShapefileDataStore shapeFileDatastore = (ShapefileDataStore) dataStore;

            CoordinateReferenceSystem crs =
                    JCRSChooser.showDialog(
                            Localisation.getString(
                                    CreateExternalDataSource.class, "CRSPanel.title"),
                            defaultCRS.getIdentifiers().iterator().next().toString());
            if (crs != null) {
                shapeFileDatastore.forceSchemaCRS(crs);
            }

            source = dataStore.getFeatureSource(typeName);
            schema = source.getSchema();
        }
    }
    dsInfo.setSchema(schema);

    determineGeometryType(schema.getGeometryDescriptor().getType());
}
 
Example 2
Source File: FeatureDataUtils.java    From geowave with Apache License 2.0 5 votes vote down vote up
public static SimpleFeatureType decodeType(
    final String nameSpace,
    final String typeName,
    final String typeDescriptor,
    final String axis) throws SchemaException {

  SimpleFeatureType featureType =
      (nameSpace != null) && (nameSpace.length() > 0)
          ? DataUtilities.createType(nameSpace, typeName, typeDescriptor)
          : DataUtilities.createType(typeName, typeDescriptor);

  final String lCaseAxis = axis.toLowerCase(Locale.ENGLISH);
  final CoordinateReferenceSystem crs = featureType.getCoordinateReferenceSystem();
  final String typeAxis = getAxis(crs);
  // Default for EPSG:4326 is lat/long, If the provided type was
  // long/lat, then re-establish the order
  if ((crs != null)
      && crs.getIdentifiers().toString().contains("EPSG:4326")
      && !lCaseAxis.equalsIgnoreCase(typeAxis)) {
    final SimpleFeatureTypeBuilder builder = new SimpleFeatureTypeBuilder();
    builder.init(featureType);

    try {
      // truely no way to force lat first
      // but it is the default in later versions of GeoTools.
      // this all depends on the authority at the time of creation
      featureType =
          SimpleFeatureTypeBuilder.retype(
              featureType,
              CRS.decode("EPSG:4326", lCaseAxis.equals("east")));
    } catch (final FactoryException e) {
      throw new SchemaException("Cannot decode EPSG:4326", e);
    }
  }
  return featureType;
}
 
Example 3
Source File: OmsScanLineRasterizer.java    From hortonmachine with GNU General Public License v3.0 4 votes vote down vote up
@Execute
public void process() throws Exception {
    checkNull(inVector);
    if (pValue == null && fCat == null) {
        throw new ModelsIllegalargumentException("One of pValue or the fCat have to be defined.", this, pm);
    }
    if (pNorth == null || pSouth == null || pWest == null || pEast == null || pRows == null || pCols == null) {
        if (inRaster == null) {
            throw new ModelsIllegalargumentException(
                    "It is necessary to supply all the information about the processing region. Did you set the boundaries and rows/cols?",
                    this, pm);
        }
    }

    if (inRaster != null) {
        RegionMap regionMap = CoverageUtilities.getRegionParamsFromGridCoverage(inRaster);
        pNorth = regionMap.getNorth();
        pSouth = regionMap.getSouth();
        pWest = regionMap.getWest();
        pEast = regionMap.getEast();
        pRows = regionMap.getRows();
        pCols = regionMap.getCols();

        inIter = CoverageUtilities.getRandomIterator(inRaster);
    }

    SimpleFeatureType schema = inVector.getSchema();
    CoordinateReferenceSystem crs = schema.getCoordinateReferenceSystem();
    GridGeometry2D pGrid;
    if (inRaster != null) {
        pGrid = inRaster.getGridGeometry();
    } else {
        pGrid = gridGeometryFromRegionValues(pNorth, pSouth, pEast, pWest, pCols, pRows, crs);
    }
    if (outWR == null) {
        paramsMap = gridGeometry2RegionParamsMap(pGrid);
        height = paramsMap.getRows();
        width = paramsMap.getCols();
        xRes = paramsMap.getXres();

        outWR = CoverageUtilities.createWritableRaster(width, height, null, null, doubleNovalue);
    }

    GeometryDescriptor geometryDescriptor = schema.getGeometryDescriptor();
    if (EGeometryType.isPoint(geometryDescriptor)) {
        throw new ModelsRuntimeException("Not implemented yet for points", this.getClass().getSimpleName());
    } else if (EGeometryType.isLine(geometryDescriptor)) {
        throw new ModelsRuntimeException("Not implemented yet for lines", this.getClass().getSimpleName());
    } else if (EGeometryType.isPolygon(geometryDescriptor)) {

        if (pUsePointInPolygon) {
            if (inRaster == null) {
                throw new ModelsIllegalargumentException("The point in polygon mode needs an input raster to work on.", this);
            }
            pm.beginTask("Prepare input data...", IHMProgressMonitor.UNKNOWN);
            List<Geometry> allGeoms = FeatureUtilities.featureCollectionToGeometriesList(inVector, false, null);
            Geometry allGeomsUnion = CascadedPolygonUnion.union(allGeoms);
            PreparedGeometry preparedGeometry = PreparedGeometryFactory.prepare(allGeomsUnion);
            pm.done();

            double value = pValue;
            pm.beginTask("Rasterizing...", height);
            WritableRandomIter wIter = CoverageUtilities.getWritableRandomIterator(outWR);
            for( int row = 0; row < height; row++ ) {
                for( int col = 0; col < width; col++ ) {
                    Coordinate coord = CoverageUtilities.coordinateFromColRow(col, row, pGrid);
                    if (preparedGeometry.intersects(gf.createPoint(coord))) {
                        wIter.setSample(col, col, 0, value);
                    }
                }
                pm.worked(1);
            }
            pm.done();
            wIter.done();
        } else {
            rasterizepolygon(pGrid);
        }
    } else {
        throw new ModelsIllegalargumentException("Couldn't recognize the geometry type of the file.",
                this.getClass().getSimpleName(), pm);
    }

    outRaster = CoverageUtilities.buildCoverage("rasterized", outWR, paramsMap,
            inVector.getSchema().getCoordinateReferenceSystem());

}
 
Example 4
Source File: OmsLinesRasterizer.java    From hortonmachine with GNU General Public License v3.0 4 votes vote down vote up
@Execute
public void process() throws Exception {
    checkNull(inVector);

    if (pNorth == null || pSouth == null || pWest == null || pEast == null || pRows == null || pCols == null) {
        throw new ModelsIllegalargumentException(
                "It is necessary to supply all the information about the processing region. Did you set the boundaries and rows/cols?",
                this, pm);
    }
    SimpleFeatureType schema = inVector.getSchema();
    CoordinateReferenceSystem crs = schema.getCoordinateReferenceSystem();
    GridGeometry2D inGrid = gridGeometryFromRegionValues(pNorth, pSouth, pEast, pWest, pCols, pRows, crs);

    if (!EGeometryType.isLine(schema.getGeometryDescriptor())) {
        throw new ModelsRuntimeException("The module works only with line vectors.", this);
    }

    RegionMap regionMap = CoverageUtilities.gridGeometry2RegionParamsMap(inGrid);
    double n = regionMap.getNorth();
    double s = regionMap.getSouth();
    double e = regionMap.getEast();
    double w = regionMap.getWest();
    double xRes = regionMap.getXres();
    double yRes = regionMap.getYres();
    double step = Math.min(xRes, yRes);

    WritableRaster outWR = CoverageUtilities.createWritableRaster(regionMap.getCols(), regionMap.getRows(), null, null,
            HMConstants.doubleNovalue);
    WritableRandomIter outIter = RandomIterFactory.createWritable(outWR, null);

    List<FeatureMate> matesList = FeatureUtilities.featureCollectionToMatesList(inVector);
    pm.beginTask("Rasterizing lines...", matesList.size());
    String fCatChecked = null;
    for( FeatureMate featureMate : matesList ) {
        Geometry geometry = featureMate.getGeometry();
        for( int i = 0; i < geometry.getNumGeometries(); i++ ) {
            Geometry geometryN = geometry.getGeometryN(i);
            List<Coordinate> lineCoordinatesAtStep = GeometryUtilities.getCoordinatesAtInterval((LineString) geometryN, step,
                    true, -1, -1);

            double cat;
            if (fCat == null) {
                cat = pCat;
            } else {
                if (fCatChecked == null) {
                    fCatChecked = FeatureUtilities.findAttributeName(featureMate.getFeature().getFeatureType(), fCat);
                    if (fCatChecked == null) {
                        throw new ModelsIllegalargumentException("Could not find an attribute named: " + fCat, this, pm);
                    }
                }
                cat = featureMate.getAttribute(fCat, Double.class);
            }

            for( Coordinate lineCoordinate : lineCoordinatesAtStep ) {
                if (!NumericsUtilities.isBetween(lineCoordinate.x, w, e)
                        || !NumericsUtilities.isBetween(lineCoordinate.y, s, n)) {
                    continue;
                }

                GridCoordinates2D onGrid = inGrid.worldToGrid(new DirectPosition2D(lineCoordinate.x, lineCoordinate.y));
                outIter.setSample(onGrid.x, onGrid.y, 0, cat);
            }
        }
        pm.worked(1);
    }
    pm.done();

    outRaster = CoverageUtilities.buildCoverage("pointsraster", outWR, regionMap, inVector.getSchema()
            .getCoordinateReferenceSystem());
}
 
Example 5
Source File: OmsVectorReshaper.java    From hortonmachine with GNU General Public License v3.0 4 votes vote down vote up
/**
 * You cannot call this once the dialog is closed, see the okPressed method.
 * @param originalFeatureType 
 * @param expressions 
 * @param names 
 * @return a SimpleFeatureType created based on the contents of Text
 */
private SimpleFeatureType createFeatureType( String expressionString, SimpleFeatureType originalFeatureType,
        List<String> names, List<Expression> expressions ) throws SchemaException {

    SimpleFeatureTypeBuilder build = new SimpleFeatureTypeBuilder();

    for( int i = 0; i < names.size(); i++ ) {
        String name = names.get(i);

        Expression expression = expressions.get(i);

        Object value = expression.evaluate(sample);

        // hack because sometimes expression returns null. I think the real bug is with
        // AttributeExpression
        Class< ? > binding = null;
        if (value == null) {
            if (expression instanceof PropertyName) {
                String path = ((PropertyName) expression).getPropertyName();
                AttributeType attributeType = sample.getFeatureType().getType(path);
                if (attributeType == null) {
                    throw new ModelsIllegalargumentException("Attribute type is null", this.getClass().getSimpleName(), pm);
                }
                binding = attributeType.getClass();
            }
        } else {
            binding = value.getClass();
        }

        if (binding == null) {
            throw new ModelsIllegalargumentException("Binding is null", this.getClass().getSimpleName(), pm);
        }

        if (Geometry.class.isAssignableFrom(binding)) {
            CoordinateReferenceSystem crs;
            AttributeType originalAttributeType = originalFeatureType.getType(name);
            if (originalAttributeType instanceof GeometryType) {
                crs = ((GeometryType) originalAttributeType).getCoordinateReferenceSystem();
            } else {
                crs = originalFeatureType.getCoordinateReferenceSystem();
            }
            build.crs(crs);

            build.add(name, binding);
        } else {
            build.add(name, binding);
        }
    }
    build.setName(getNewTypeName(originalFeatureType.getTypeName()));

    return build.buildFeatureType();
}