org.geotools.map.FeatureLayer Java Examples

The following examples show how to use org.geotools.map.FeatureLayer. 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: MapRender.java    From sldeditor with GNU General Public License v3.0 6 votes vote down vote up
/**
 * Render vector symbol.
 *
 * @param mapContent the map content
 * @param styledLayer the styled layer
 * @param style the style
 */
private void renderVectorSymbol(MapContent mapContent, StyledLayer styledLayer, Style style) {
    FeatureSource<SimpleFeatureType, SimpleFeature> tmpFeatureList = null;

    if (styledLayer instanceof UserLayer) {
        if (userLayerFeatureListMap != null) {
            tmpFeatureList = userLayerFeatureListMap.get(styledLayer);
        }
    } else {
        tmpFeatureList = featureList;
    }

    if (tmpFeatureList != null) {
        mapContent.addLayer(
                new FeatureLayer(tmpFeatureList, (org.geotools.styling.Style) style));
        try {
            mapPane.setDisplayArea(tmpFeatureList.getBounds());
        } catch (IOException e) {
            ConsoleManager.getInstance().exception(this, e);
        }
    }
}
 
Example #2
Source File: MapUtils.java    From dhis2-core with BSD 3-Clause "New" or "Revised" License 6 votes vote down vote up
/**
 * Creates a feature layer based on a map object.
 */
public static Layer createFeatureLayerFromMapObject( InternalMapObject mapObject )
{
    Style style = mapObject.getStyle();
    
    SimpleFeatureType featureType = mapObject.getFeatureType();
    SimpleFeatureBuilder featureBuilder = new SimpleFeatureBuilder( featureType );
    DefaultFeatureCollection featureCollection = new DefaultFeatureCollection();
    
    featureBuilder.add( mapObject.getGeometry() );
    SimpleFeature feature = featureBuilder.buildFeature( null );

    featureCollection.add( feature );

    return new FeatureLayer( featureCollection, style );
}
 
Example #3
Source File: RenderPanelImpl.java    From sldeditor with GNU General Public License v3.0 5 votes vote down vote up
/**
 * Render vector map.
 *
 * @param features the results
 * @param imageSize the image size
 * @param style the style
 * @param dpi the dpi
 */
private void renderVectorMap(
        FeatureSource<SimpleFeatureType, SimpleFeature> features,
        Rectangle imageSize,
        Style style,
        int dpi) {
    List<Layer> layerList = new ArrayList<>();
    if (style != null) {
        FeatureLayer featureLayer = new FeatureLayer(features, style);
        layerList.add(featureLayer);
    }

    boolean hasGeometry = false;
    ReferencedEnvelope bounds = null;

    if (features != null) {
        bounds = calculateBounds();

        wmsEnvVarValues.setMapBounds(bounds);

        EnvironmentVariableManager.getInstance().setWMSEnvVarValues(wmsEnvVarValues);

        if (features.getSchema() != null) {
            hasGeometry = (features.getSchema().getGeometryDescriptor() != null);
        }
    }

    internalRenderMap(layerList, bounds, imageSize, hasGeometry, dpi);
}
 
Example #4
Source File: ShapeFileViewer.java    From gama with GNU General Public License v3.0 5 votes vote down vote up
@Override
public void init(final IEditorSite site, final IEditorInput input) throws PartInitException {
	setSite(site);
	final FileEditorInput fi = (FileEditorInput) input;
	file = fi.getFile();
	final IPath path = fi.getPath();
	final File f = path.makeAbsolute().toFile();
	try {
		pathStr = f.getAbsolutePath();
		final ShapefileDataStore store = new ShapefileDataStore(f.toURI().toURL());
		store.setCharset(Charset.forName("UTF8"));
		content = new MapContent();
		featureSource = store.getFeatureSource();
		style = Utils.createStyle2(featureSource);
		layer = new FeatureLayer(featureSource, style);
		mode = determineMode(featureSource.getSchema(), "Polygon");
		final List<FeatureTypeStyle> ftsList = style.featureTypeStyles();
		if (ftsList.size() > 0) {
			fts = ftsList.get(0);
		} else {
			fts = null;
		}
		if (fts != null) {
			this.setFillColor(PreferencesHelper.SHAPEFILE_VIEWER_FILL.getValue(), mode, fts);
			this.setStrokeColor(PreferencesHelper.SHAPEFILE_VIEWER_LINE_COLOR.getValue(), mode, fts);
			((StyleLayer) layer).setStyle(style);
		}
		content.addLayer(layer);
	} catch (final IOException e) {
		DEBUG.ERR("Unable to view file " + path);
	}
	this.setPartName(path.lastSegment());
	setInput(input);
}
 
Example #5
Source File: RasterizedShapefilesFolderNwwLayer.java    From hortonmachine with GNU General Public License v3.0 5 votes vote down vote up
private static GTRenderer getRenderer(File shapeFilesFolder) {
    File[] shpFiles = shapeFilesFolder.listFiles(new FilenameFilter() {
        @Override
        public boolean accept(File dir, String name) {
            return name.toLowerCase().endsWith(".shp");
        }
    });

    MapContent mapContent = new MapContent();
    for (File shpFile : shpFiles) {
        try {
            SimpleFeatureCollection readFC = NwwUtilities.readAndReproject(shpFile.getAbsolutePath());
            ReferencedEnvelope tmpBounds = readFC.getBounds();
            if (tmpBounds.getWidth() == 0 || tmpBounds.getHeight() == 0) {
                System.err.println("Ignoring: " + shpFile);
                continue;
            }
            // if (bounds == null) {
            // bounds = new ReferencedEnvelope(tmpBounds);
            // } else {
            // bounds.expandToInclude(tmpBounds);
            // }
            Style style = SldUtilities.getStyleFromFile(shpFile);
            if (style == null)
                style = SLD.createSimpleStyle(readFC.getSchema());

            FeatureLayer layer = new FeatureLayer(readFC, style);
            mapContent.addLayer(layer);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
    GTRenderer renderer = new StreamingRenderer();
    renderer.setMapContent(mapContent);
    return renderer;
}
 
Example #6
Source File: RasterizedFeatureCollectionLayer.java    From hortonmachine with GNU General Public License v3.0 5 votes vote down vote up
private static GTRenderer getRenderer( SimpleFeatureCollection featureCollectionLL, Style style ) {
    MapContent mapContent = new MapContent();
    try {
        FeatureLayer layer = new FeatureLayer(featureCollectionLL, style);
        mapContent.addLayer(layer);
    } catch (Exception e) {
        e.printStackTrace();
    }
    GTRenderer renderer = new StreamingRenderer();
    renderer.setMapContent(mapContent);
    return renderer;
}
 
Example #7
Source File: RasterizedSpatialiteLayer.java    From hortonmachine with GNU General Public License v3.0 5 votes vote down vote up
private static GTRenderer getRenderer( ASpatialDb db, String tableName, int featureLimit, Style style ) {
    MapContent mapContent = new MapContent();

    // read data and convert it to featurecollection
    try {
        long t1 = System.currentTimeMillis();
        System.out.println("STARTED READING: " + tableName);

        String databasePath = db.getDatabasePath();
        File dbFile = new File(databasePath);
        File parentFolder = dbFile.getParentFile();
        if (style == null) {
            File sldFile = new File(parentFolder, tableName + ".sld");
            if (sldFile.exists()) {
                style = SldUtilities.getStyleFromFile(sldFile);
            }
        }

        DefaultFeatureCollection fc = SpatialDbsImportUtils.tableToFeatureFCollection(db, tableName, featureLimit,
                NwwUtilities.GPS_CRS_SRID, null);
        long t2 = System.currentTimeMillis();
        System.out.println("FINISHED READING: " + tableName + " -> " + ((t2 - t1) / 1000) + "sec");
        if (style == null) {
            style = SLD.createSimpleStyle(fc.getSchema());
        }
        FeatureLayer layer = new FeatureLayer(fc, style);

        long t3 = System.currentTimeMillis();
        System.out.println("FINISHED BUILDING: " + tableName + " -> " + ((t3 - t2) / 1000) + "sec");

        mapContent.addLayer(layer);
    } catch (Exception e) {
        e.printStackTrace();
    }
    GTRenderer renderer = new StreamingRenderer();
    renderer.setMapContent(mapContent);
    return renderer;
}
 
Example #8
Source File: OmsMapsViewer.java    From hortonmachine with GNU General Public License v3.0 4 votes vote down vote up
private void addFeatureCollections( MapContent map ) throws Exception {
    if (inVectors == null) {
        return;
    }
    for( String path : inVectors ) {
        SimpleFeatureCollection fc = OmsVectorReader.readVector(path);
        GeometryDescriptor geometryDescriptor = fc.getSchema().getGeometryDescriptor();
        EGeometryType type = EGeometryType.forGeometryDescriptor(geometryDescriptor);

        File file = new File(path);
        Style style = SldUtilities.getStyleFromFile(file);

        switch( type ) {
        case MULTIPOLYGON:
        case POLYGON:
            if (style == null) {
                Stroke polygonStroke = sf.createStroke(ff.literal(Color.BLUE), ff.literal(2));
                Fill polygonFill = sf.createFill(ff.literal(Color.BLUE), ff.literal(0.0));

                Rule polygonRule = sf.createRule();
                PolygonSymbolizer polygonSymbolizer = sf.createPolygonSymbolizer(polygonStroke, polygonFill, null);
                polygonRule.symbolizers().add(polygonSymbolizer);

                FeatureTypeStyle polygonFeatureTypeStyle = sf.createFeatureTypeStyle();
                polygonFeatureTypeStyle.rules().add(polygonRule);

                style = sf.createStyle();
                style.featureTypeStyles().add(polygonFeatureTypeStyle);
                style.setName("polygons");
            }
            break;
        case MULTIPOINT:
        case POINT:
            if (style == null) {
                Mark circleMark = sf.getCircleMark();
                Fill fill = sf.createFill(ff.literal(Color.RED));
                circleMark.setFill(fill);
                // circleMark.setStroke(null);

                Graphic gr = sf.createDefaultGraphic();
                gr.graphicalSymbols().clear();
                gr.graphicalSymbols().add(circleMark);
                Expression size = ff.literal(6);
                gr.setSize(size);

                Rule pointRule = sf.createRule();
                PointSymbolizer pointSymbolizer = sf.createPointSymbolizer(gr, null);
                pointRule.symbolizers().add(pointSymbolizer);

                FeatureTypeStyle pointsFeatureTypeStyle = sf.createFeatureTypeStyle();
                pointsFeatureTypeStyle.rules().add(pointRule);

                style = sf.createStyle();
                style.featureTypeStyles().add(pointsFeatureTypeStyle);
                style.setName("points");
            }
            break;
        case MULTILINESTRING:
        case LINESTRING:
            if (style == null) {
                Stroke lineStroke = sf.createStroke(ff.literal(Color.RED), ff.literal(2));

                Rule lineRule = sf.createRule();
                LineSymbolizer lineSymbolizer = sf.createLineSymbolizer(lineStroke, null);
                lineRule.symbolizers().add(lineSymbolizer);

                FeatureTypeStyle lineFeatureTypeStyle = sf.createFeatureTypeStyle();
                lineFeatureTypeStyle.rules().add(lineRule);

                style = sf.createStyle();
                style.featureTypeStyles().add(lineFeatureTypeStyle);
                style.setName("lines");
            }
            break;

        default:
            break;
        }

        FeatureLayer layer = new FeatureLayer(fc, style);
        map.addLayer(layer);

    }

}
 
Example #9
Source File: HMMapframe.java    From hortonmachine with GNU General Public License v3.0 4 votes vote down vote up
public void addLayer( SimpleFeatureCollection featureCollection ) {
    FeatureLayer fl = makeFeatureLayer(featureCollection);
    addLayer(fl);
}
 
Example #10
Source File: HMMapframe.java    From hortonmachine with GNU General Public License v3.0 4 votes vote down vote up
public void setLayer( SimpleFeatureCollection featureCollection ) {
    FeatureLayer fl = makeFeatureLayer(featureCollection);
    setLayer(fl);
}
 
Example #11
Source File: HMMapframe.java    From hortonmachine with GNU General Public License v3.0 4 votes vote down vote up
public void addLayer( SimpleFeatureCollection featureCollection, Style style ) {
    FeatureLayer fl = new FeatureLayer(featureCollection, style);
    addLayer(fl);
}
 
Example #12
Source File: HMMapframe.java    From hortonmachine with GNU General Public License v3.0 4 votes vote down vote up
public void setLayer( SimpleFeatureCollection featureCollection, Style style ) {
    FeatureLayer fl = new FeatureLayer(featureCollection, style);
    setLayer(fl);
}
 
Example #13
Source File: HMMapframe.java    From hortonmachine with GNU General Public License v3.0 4 votes vote down vote up
private FeatureLayer makeFeatureLayer( SimpleFeatureCollection featureCollection ) {
    Style style = StyleUtilities.createDefaultStyle(featureCollection);
    FeatureLayer fl = new FeatureLayer(featureCollection, style);
    return fl;
}
 
Example #14
Source File: DistributedRenderCallback.java    From geowave with Apache License 2.0 4 votes vote down vote up
@Override
public Layer beforeLayer(final WMSMapContent mapContent, final Layer layer) {
  // sanity check the style
  if ((layer instanceof FeatureLayer)
      && (layer.getStyle() != null)
      && (layer.getStyle().featureTypeStyles() != null)
      && !layer.getStyle().featureTypeStyles().isEmpty()) {

    final Style layerStyle = layer.getStyle();
    final FeatureTypeStyle style = layerStyle.featureTypeStyles().get(0);
    // check if their is a DistributedRender rendering
    // transformation
    if ((style instanceof ProcessFunction)
        && (style.getTransformation() != null)
        && (((ProcessFunction) style.getTransformation()).getName() != null)
        && ((ProcessFunction) style.getTransformation()).getName().equals(
            DistributedRenderProcess.PROCESS_NAME)) {
      // if their is a DistributedRender transformation, we need
      // to provide more information that can only be found
      final DuplicatingStyleVisitor cloner = new DuplicatingStyleVisitor();
      layerStyle.accept(cloner);
      layer.getQuery().getHints().put(
          DistributedRenderProcess.OPTIONS,
          new DistributedRenderOptions(wms, mapContent, layerStyle));
      // now that the options with the distributed render style
      // have been set the original style will be used with
      // distributed rendering

      // now, replace the style with a direct raster symbolizer,
      // so the GridCoverage result of the distributed rendering
      // process is directly rendered to the map in place of the
      // original style

      final Style directRasterStyle = (Style) cloner.getCopy();
      directRasterStyle.featureTypeStyles().clear();
      Processors.addProcessFactory(new InternalProcessFactory());
      directRasterStyle.featureTypeStyles().add(
          getDirectRasterStyle(
              layer.getFeatureSource().getSchema().getGeometryDescriptor().getLocalName(),
              DistributedRenderProcessUtils.getRenderingProcess()));
      ((FeatureLayer) layer).setStyle(directRasterStyle);
    }
  }
  return layer;
}
 
Example #15
Source File: DistributedRenderAggregation.java    From geowave with Apache License 2.0 4 votes vote down vote up
private void initRenderer(final SimpleFeatureType type) {
  currentRenderer = new DistributedRenderMapOutputFormat(options);
  final WMSMapContent mapContent = new WMSMapContent();
  final GetMapRequest request = new GetMapRequest();
  mapContent.setBgColor(options.getBgColor());
  request.setBgColor(options.getBgColor());
  mapContent.setPalette(options.getPalette());
  request.setPalette(options.getPalette());
  mapContent.setAngle(options.getAngle());
  request.setAngle(options.getAngle());
  mapContent.setBuffer(options.getBuffer());
  request.setBuffer(options.getBuffer());
  mapContent.setMapWidth(options.getMapWidth());
  request.setWidth(options.getMapWidth());
  mapContent.setMapHeight(options.getMapHeight());
  request.setHeight(options.getMapHeight());
  mapContent.setTransparent(options.isTransparent());
  request.setTransparent(options.isTransparent());
  mapContent.setViewport(new MapViewport(options.getEnvelope()));
  request.setBbox(options.getEnvelope());
  request.setInterpolations(options.getInterpolations());
  final Map formatOptions = new HashMap<>();
  formatOptions.put("antialias", options.getAntialias());
  formatOptions.put("timeout", options.getMaxRenderTime());
  formatOptions.put("kmplacemark", Boolean.valueOf(options.isKmlPlacemark()));
  // this sets a static variable, but its the only method available
  // (multiple geoserver clients with different settings hitting the same
  // distributed backend, may conflict on these settings)

  // we get around this by overriding these settings on the renderHints
  // object within DistributedRenderer so it is no longer using these
  // static settings, but these static properties must be set to avoid
  // NPEs
  System.setProperty("OPTIMIZE_LINE_WIDTH", Boolean.toString(options.isOptimizeLineWidth()));
  System.setProperty("MAX_FILTER_RULES", Integer.toString(options.getMaxFilters()));
  System.setProperty(
      "USE_GLOBAL_RENDERING_POOL",
      Boolean.toString(DistributedRenderOptions.isUseGlobalRenderPool()));
  new DefaultWebMapService(null).setApplicationContext(null);
  request.setFormatOptions(formatOptions);
  request.setWidth(options.getMapWidth());
  request.setHeight(options.getMapHeight());
  request.setTiled(options.isMetatile());
  request.setScaleMethod(
      options.isRenderScaleMethodAccurate() ? ScaleComputationMethod.Accurate
          : ScaleComputationMethod.OGC);

  if (options.isMetatile()) {
    // it doesn't matter what this is, as long as its not null, we are
    // just ensuring proper transparency usage based on meta-tiling
    // rules
    request.setTilesOrigin(new Point2D.Double());
  }
  mapContent.setRequest(request);
  queue = new Queue<>();
  mapContent.addLayer(
      new FeatureLayer(new AsyncQueueFeatureCollection(type, queue), options.getStyle()));
  // produce map in a separate thread...
  asyncRenderer = CompletableFuture.supplyAsync(() -> {
    currentRenderer.produceMap(mapContent).dispose();
    return currentRenderer.getDistributedRenderResult();
  });
}