Creating a raster file from a shapefile using GeoTools with different colours for dif

المشرف العام

Administrator
طاقم الإدارة
The shapefiles that I am processing display information on an operation of a piece of equipment over an area, where the information is stored in steps as polygon features with various attributes and values. My goal is to present the information using a colour spectrum so the colors represent the value of a chosen attribute.

So for example, lets say my shapefile describes the operation of a vacuum cleaner over an imaginery 10km^2 house, where the vacuum cleaner sends data every 50cm regarding how much dust it sucked in, how wet the dust was, how many human hairs were found there etc and stores each step as a feature with a sequence number and all these attributes and values.

Now as far as I've seen, the only way to achieve different colors is to use different layers, as a layer can only have one style, so in order to do this I need to have a layer per feature, so in the given example that means I will have (1000/50)^2=4000 layers which is pretty heavy (the actual data that I am processing is of this magnitude, so real scenario). This becomes a problem in two specific places:

  1. Where I am calling MapContent.getMaxBounds(), this requires iteration over the features
  2. Where I am rendering the graphics, as that also requires such an iteration. So for a shapefile with about 4000 features (which I am using in my test case) it takes about 10 minutes.
My question is: am I approaching this right? Is there a better way to colour polygon features differently? Is there a way where I dont have to create a layer per feature?

Here's my code:

/** * Create the geotiff image based on featureCollection and the resolution params for an numerical value attribute. * * @param featureCollection * @param resolution * @return byte[] * @throws IOException */public static byte[] createGeotiffImageForNumericalValues(SimpleFeatureCollection featureCollection, Rectangle resolution, String attributeName, String feaureUniqueIdentifier) throws IOException { MapContent map = new MapContent(); map.setTitle("GeoTiff"); featureCollection.features(); SimpleFeatureIterator iterator = featureCollection.features(); SimpleFeature feature; Collection properties; float valueDivided; double highestValue = extractHighestValue(iterator, attributeName); //this is for creating the colour spectrum, used later to create the style Object val; iterator = featureCollection.features(); Color fillerColor; FeatureLayer featureLayer; FilterFactory ff = CommonFactoryFinder.getFilterFactory(null); try { while (iterator.hasNext()) { feature = iterator.next(); SimpleFeatureType type = feature.getType(); properties = feature.getProperties(); for (Property property : properties) { String propName = property.getName().getLocalPart(); if (propName.equals(attributeName)) { val = property.getValue(); if (val == null) { continue; } valueDivided = new Float((double) val / highestValue); fillerColor = new Color(0, 1, 0, valueDivided); featureLayer = new FeatureLayer(featureCollection, SLD.createPolygonStyle(new Color(1, 1, 1), fillerColor, (float) 0.5)); Filter fil = ff.equals(ff.property(feaureUniqueIdentifier), ff.literal(feature.getProperty(feaureUniqueIdentifier).getValue())); featureLayer.setQuery(new Query(type.getName().getLocalPart(), fil)); map.addLayer(featureLayer); } } } } finally { iterator.close(); } Rectangle screen = new Rectangle(1500, 1500); StreamingRenderer renderer = new StreamingRenderer(); ReferencedEnvelope maxBounds = map.getMaxBounds(); //
 
أعلى