|
From: jv <cib...@gm...> - 2009-09-09 08:07:37
|
You both really rock :-) I don't know if I will be able to find my way
around the api but knowing you're so active in the mailing list really makes
me think this project has a cool future.
I know part of my problem is that I don't have enough background information
(for example, I mix the concept of style and actual symbol on map placed at
a determined coordinate). So don't take the next code very seriously, but I
hope somebody will find it interesting: it creates layer from a series of
coordinates placing a polygon of the indicated size for each xy pair.
Depending of a parameter call fiability the color of the polygon changes.
import com.vividsolutions.jts.geom.Coordinate;
import com.vividsolutions.jts.geom.GeometryFactory;
import com.vividsolutions.jts.geom.LinearRing;
import com.vividsolutions.jts.geom.Polygon;
import edu.ub.bio.framework.SystemException;
import edu.ub.bio.wms.model.Fiability;
import java.awt.Color;
import java.io.IOException;
import java.util.HashMap;
import java.util.Map;
import org.geotools.data.memory.MemoryDataStore;
import org.geotools.feature.FeatureCollection;
import org.geotools.feature.simple.SimpleFeatureBuilder;
import org.geotools.feature.simple.SimpleFeatureTypeBuilder;
import org.geotools.geometry.jts.JTSFactoryFinder;
import org.geotools.map.DefaultMapLayer;
import org.geotools.map.MapLayer;
import org.geotools.styling.Fill;
import org.geotools.styling.PolygonSymbolizer;
import org.geotools.styling.Style;
import org.geotools.styling.StyleBuilder;
import org.opengis.feature.simple.SimpleFeature;
import org.opengis.feature.simple.SimpleFeatureType;
import org.opengis.referencing.crs.CoordinateReferenceSystem;
/**
*
* @author ciberado
*/
public class UTMMapLayerBuilder {
private MemoryDataStore dataStore;
private CoordinateReferenceSystem crs;
private Map<Fiability, SimpleFeatureType> featureTypes =
new HashMap<Fiability, SimpleFeatureType>();
public UTMMapLayerBuilder(CoordinateReferenceSystem crs) {
this.dataStore = new MemoryDataStore();
this.crs = crs;
for (Fiability fiab : Fiability.values()) {
featureTypes.put(fiab, createSpeciesFeatureType(fiab));
}
}
public void addSquare(String id, String name, double x, double y,
double size, Fiability fiability) {
Polygon polygon = createPolygon(x, y, size);
SimpleFeature feature = SimpleFeatureBuilder.build(
featureTypes.get(fiability),
new Object[]{polygon, name}, id);
dataStore.addFeature(feature);
}
public MapLayer buildLayer(Color color) {
try {
DefaultMapLayer layer = null;
for (String currentName : dataStore.getTypeNames()) {
StyleBuilder builder = new StyleBuilder();
PolygonSymbolizer symbolizer = builder.createPolygonSymbolizer(color);
Fiability fiability = Fiability.valueOf(currentName);
Fill fill = createFillByFiability(builder, color, fiability);
symbolizer.setFill(fill);
Style style = builder.createStyle(symbolizer);
FeatureCollection featureCollection =
dataStore.getFeatureSource(currentName).getFeatures();
layer = new DefaultMapLayer(featureCollection, style);
layer.setTitle("SpeciesList");
}
return layer;
} catch (IOException e) {
throw new SystemException(e);
}
}
private Fill createFillByFiability(StyleBuilder builder, Color color,
Fiability fiability) {
Fill fill;
switch (fiability) {
case DISAPPEARED : fill = builder.createFill(Color.BLACK, 0.33); break;
case SURE : fill = builder.createFill(Color.GREEN, 0.33); break;
case UNSURE : fill = builder.createFill(Color.ORANGE, 0.33); break;
case UNKNOWN : fill = builder.createFill(Color.BLUE, 0.33); break;
default: fill = null;
}
return fill;
}
private SimpleFeatureType createSpeciesFeatureType(Fiability fiability) {
SimpleFeatureTypeBuilder builder = new SimpleFeatureTypeBuilder();
builder.setName(fiability.name());
builder.setNamespaceURI("http://biodiver.bio.ub.es/geo");
builder.setCRS(crs);
builder.add("squareGeometry", Polygon.class);
builder.add("speciesName", String.class);
SimpleFeatureType featureType = builder.buildFeatureType();
return featureType;
}
private static Polygon createPolygon(double x, double y, double size) {
GeometryFactory gf = JTSFactoryFinder.getGeometryFactory(null);
Coordinate[] polygonCoordinates = new Coordinate[5];
polygonCoordinates[0] = new Coordinate(x, y);
polygonCoordinates[1] = new Coordinate(x + size, y);
polygonCoordinates[2] = new Coordinate(x + size, y + size);
polygonCoordinates[3] = new Coordinate(x, y + size);
polygonCoordinates[4] = polygonCoordinates[0];
LinearRing ring = gf.createLinearRing(polygonCoordinates);
Polygon polygon = gf.createPolygon(ring, null);
return polygon;
}
}
On Wed, Sep 9, 2009 at 9:48 AM, Jody Garnett <jod...@gm...> wrote:
> Good call Micheal - so let us ask the user list here for a shopping
> list of examples; and once we get 2.6-M3 out the door we can swap code
> examples back and forth in order to refine the docs; and the api.
>
> I think styles also come across as hard as that is where the user
> guide stops :-( Some code examples will go a long way here.
>
> With that in mind here is an example of putting together a style with
> an external graphic:
> StyleBuilder styleBuilder = new StyleBuilder();
> Style style = styleBuilder.createStyle();
> {
> { PointSymbolizer pointSymbolizer =
> styleBuilder.createPointSymbolizer();
>
> { Graphic graphic = styleBuilder.createGraphic();
> ExternalGraphic external =
> styleBuilder.createExternalGraphic( "file:///C:/images/house.gif",
> "image/gif");
> graphic.graphicalSymbols().add( external );
> graphic.graphicalSymbols().add(
> styleBuilder.createMark("circle"));
>
> pointSymbolizer.setGraphic(graphic);
> }
> Rule rule = styleBuilder.createRule(pointSymbolizer);
> FeatureTypeStyle featureTypeStyle =
> styleBuilder.createFeatureTypeStyle("Feature", rule );
> style.featureTypeStyles().add( featureTypeStyle );
> }
> }
>
> I have added this to the page
> http://docs.codehaus.org/display/GEOTDOC/04+Styling
> Jody
>
> On Wed, Sep 9, 2009 at 12:16 PM, Michael
> Bedward<mic...@gm...> wrote:
> > 2009/9/9 Jody Garnett <jod...@gm...>:
> >> These examples will be in XML but the data structure being represented
> is
> >> the same.
> >
> > But I think that illustrates part of the problem: you need to know
> > GeoTools style API quite well to translate XML examples to
> > programmatic examples in your head. So if you are new to the library
> > or styling it's catch-22.
> >
> > Personally, I've found styling to be one of the most difficult aspects
> > of the library to master and, in part, I think that has been because
> > so much of the information here and elsewhere is written for users of
> > GeoServer and similar tools. There is relatively little about
> > programmatic use in stand-alone apps.
> >
> > This is an observation rather than a criticism. Everyone here
> > struggles to find time to work on the docs.
> >
> > Michael
> >
>
--
=======================================================
Javier Moreno
www.imaginafoto.com
=======================================================
|