On 26/04/10 19:09, Andrea Aime wrote:
>
> Just curious if you looked into the question at all. A WKT based mark
> factory is fine by me ;-)

That I did, including some tests (patches at the end of this message); of 
course, 
some user doc will have to be written too. By the way, the source code includes 
some more well-known shapes that I've found useful.

As per SVG, I've found out some "orphan" code that may be of interest 
(http://www.mail-archive.com/[email protected]/msg04729.html).

If you deem it worthy, I may try to adapt it to GeoTools.

Regards,

--------------------
    Luca Morandini
www.lucamorandini.it
--------------------

### Eclipse Workspace Patch 1.0
#P geotools-2.6.x
Index: 
modules/library/render/src/main/java/org/geotools/renderer/style/ShapeMarkFactory.java
===================================================================
--- 
modules/library/render/src/main/java/org/geotools/renderer/style/ShapeMarkFactory.java
 
(revision 35265)
+++ 
modules/library/render/src/main/java/org/geotools/renderer/style/ShapeMarkFactory.java
 
(working copy)
@@ -27,12 +27,16 @@
  import java.util.logging.Logger;

  import org.geotools.renderer.style.shape.ExplicitBoundsShape;
+
+import org.geotools.geometry.jts.LiteShape;
+import org.geotools.geometry.jts.WKTReader2;
  import org.opengis.feature.Feature;
  import org.opengis.filter.expression.Expression;

  public class ShapeMarkFactory implements MarkFactory {

      private static final String SHAPE_PREFIX = "shape://";
+    private static final String WKT_PREFIX = "wkt://";

      /** The logger for the rendering module. */
      private static final Logger LOGGER = 
org.geotools.util.logging.Logging.getLogger(
@@ -63,20 +67,74 @@
         gp.moveTo(-0.5f, -0.5f);
         gp.lineTo(0.5f, 0.5f);
         shapes.put("times", gp);
+
+       gp = new GeneralPath();
+       gp.moveTo(-0.5f, -0.125f);
+       gp.lineTo(-0.125f, -0.125f);
+       gp.lineTo(0.0f, 0.125f);
+       gp.lineTo(0.125f, -0.125f);
+       gp.lineTo(0.5f, -0.125f);
+       gp.closePath();
+       shapes.put("triangle", gp);
+       
+       gp = new GeneralPath();
+       gp.moveTo(-0.5f, -0.125f);
+       gp.lineTo(-0.125f, -0.125f);
+
+       for (float i=8.0f; i >= 0.0f; i=i -1.0f ) {
+           gp.lineTo(
+                   (float)(0.0f + Math.cos(Math.PI * i / 8.0f) * 0.25f),
+                   (float)(-0.125f + Math.sin(Math.PI * i / 8.0f) * 0.25f)
+                   );
+       }
+       
+       gp.lineTo(0.5f, -0.125f);
+       gp.closePath();
+       shapes.put("semicircle", gp);
+
+       gp = new GeneralPath();
+       gp.moveTo(-0.5f, -0.125f);
+       gp.lineTo(-0.375f, -0.125f);
+       
+       for (float i=8.0f; i >= 0.0f; i=i -1.0f ) {
+           gp.lineTo(
+                   (float)(-0.25f + Math.cos(Math.PI * i / 8.0f) * 0.125f),
+                   (float)(-0.125f + Math.sin(Math.PI * i / 8.0f) * 0.125f)
+                   );
+       }
+       
+       gp.lineTo(0.125f, -0.125f);
+       gp.lineTo(0.25f, -0.375f);
+       gp.lineTo(0.375f, -0.125f);
+       gp.lineTo(0.5f, -0.125f);
+       gp.closePath();
+       shapes.put("trianglesemicircle", gp);
      }

      public Shape getShape(Graphics2D graphics, Expression symbolUrl, Feature 
feature) throws Exception {
+       
          // cannot handle a null url
-        if(symbolUrl == null)
+        if(symbolUrl == null) {
              return null;
+        }

-        // see if it's a shape://
-        String wellKnownName = symbolUrl.evaluate(feature, String.class);
-        if(!wellKnownName.startsWith(SHAPE_PREFIX))
-            return null;
+        // see if it's a shape
+        String wellKnown = symbolUrl.evaluate(feature, String.class);
+        if ( wellKnown.startsWith(SHAPE_PREFIX)) {

-        String name = wellKnownName.substring(SHAPE_PREFIX.length());
-        return shapes.get(name);
+            String wellKnownName = wellKnown.substring(SHAPE_PREFIX.length());
+            return shapes.get(wellKnownName);
+        }
+
+        // Otherwise, See if it is a WKT
+        if ( wellKnown.startsWith(WKT_PREFIX)) {
+            String wellKnownText = wellKnown.substring(WKT_PREFIX.length());
+            WKTReader2 reader= new WKTReader2();
+            LiteShape shape= new LiteShape(reader.read(wellKnownText), null, 
false);
+            return shape;
+        }
+
+        return null;
      }

-}
+}
\ No newline at end of file



### Eclipse Workspace Patch 1.0
#P geotools-2.6.x
Index: 
modules/library/render/src/test/java/org/geotools/renderer/style/ShapeMarkFactoryTest.java
===================================================================
--- 
modules/library/render/src/test/java/org/geotools/renderer/style/ShapeMarkFactoryTest.java
 
(revision 0)
+++ 
modules/library/render/src/test/java/org/geotools/renderer/style/ShapeMarkFactoryTest.java
 
(revision 0)
@@ -0,0 +1,148 @@
+/*
+ *    GeoTools - The Open Source Java GIS Toolkit
+ *    http://geotools.org
+ *
+ *    (C) 2002-2008, Open Source Geospatial Foundation (OSGeo)
+ *
+ *    This library is free software; you can redistribute it and/or
+ *    modify it under the terms of the GNU Lesser General Public
+ *    License as published by the Free Software Foundation;
+ *    version 2.1 of the License.
+ *
+ *    This library is distributed in the hope that it will be useful,
+ *    but WITHOUT ANY WARRANTY; without even the implied warranty of
+ *    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
+ *    Lesser General Public License for more details.
+ */
+
+package org.geotools.renderer.style;
+
+import org.opengis.feature.simple.SimpleFeature;
+import org.opengis.feature.simple.SimpleFeatureType;
+import org.opengis.filter.expression.Expression;
+import org.geotools.factory.CommonFactoryFinder;
+import org.geotools.feature.simple.SimpleFeatureBuilder;
+import org.geotools.feature.simple.SimpleFeatureTypeBuilder;
+import org.opengis.filter.FilterFactory;
+import org.geotools.referencing.crs.DefaultGeographicCRS;
+
+import com.vividsolutions.jts.geom.LineString;
+
+import junit.framework.TestCase;
+
+/**
+ * Unit tests for shape mark factory
+ *
+ * @author Luca Morandini [email protected]
+ *
+ * @source $URL:$
+ */
+public class ShapeMarkFactoryTest extends TestCase {
+
+    private SimpleFeature feature;
+    private Expression exp;
+    private FilterFactory ff;
+
+    {
+       try {
+           ff = CommonFactoryFinder.getFilterFactory(null);
+           SimpleFeatureTypeBuilder featureTypeBuilder = new 
SimpleFeatureTypeBuilder();
+           featureTypeBuilder.setName("TestType");
+           featureTypeBuilder.add("geom", LineString.class,
+                   DefaultGeographicCRS.WGS84);
+           SimpleFeatureType featureType = featureTypeBuilder
+                   .buildFeatureType();
+           SimpleFeatureBuilder featureBuilder = new SimpleFeatureBuilder(
+                   featureType);
+           this.feature = featureBuilder.buildFeature(null);
+       } catch (Exception e) {
+           e.printStackTrace();
+       }
+    }
+
+    public void testWellKnownName() {
+       ShapeMarkFactory smf = new ShapeMarkFactory();
+       try {
+           this.exp = ff.literal("shape://plus");
+           smf.getShape(null, this.exp, this.feature);
+       } catch (Exception e) {
+           assertTrue(false);
+           return;
+       }
+
+       assertTrue(true);
+    }
+
+    public void testWellKnownTextLineString() {
+       ShapeMarkFactory smf = new ShapeMarkFactory();
+       try {
+           this.exp = ff
+                   .literal("wkt://LINESTRING(0.0 0.25, 0.25 0.25, 0.5 0.75, 
0.75 0.25, 1.00 
0.25)");
+           smf.getShape(null, this.exp, this.feature);
+       } catch (Exception e) {
+           assertTrue(false);
+           return;
+       }
+
+       assertTrue(true);
+    }
+
+    public void testWellKnownTextMultiLineString() {
+       ShapeMarkFactory smf = new ShapeMarkFactory();
+       try {
+           this.exp = ff
+                   .literal("wkt://MULTILINESTRING((0.25 0.25, 0.5 0.75, 0.75 
0.25, 0.25 
0.25), (0.25 0.75, 0.5 0.25, 0.75 0.75, 0.25 0.75))");
+           smf.getShape(null, this.exp, this.feature);
+       } catch (Exception e) {
+           assertTrue(false);
+           return;
+       }
+
+       assertTrue(true);
+    }
+
+    public void testWellKnownTextPolygon() {
+       ShapeMarkFactory smf = new ShapeMarkFactory();
+       try {
+           this.exp = ff
+                   .literal("wkt://POLYGON((0.25 0.25, 0.5 0.75, 0.75 0.25, 
0.25 0.25))");
+           smf.getShape(null, this.exp, this.feature);
+       } catch (Exception e) {
+           assertTrue(false);
+           return;
+       }
+
+       assertTrue(true);
+    }
+
+    public void testWellKnownTextPolygonError() {
+       ShapeMarkFactory smf = new ShapeMarkFactory();
+       try {
+           this.exp = ff
+                   .literal("wkt://POLYGON((0.25 0.25, 0.5 0.75, 0.75 0.25,    
 ))");
+           smf.getShape(null, this.exp, this.feature);
+       } catch (Exception e) {
+           assertTrue(true);
+           return;
+       }
+
+       assertTrue(false);
+    }
+
+    public void testUnknownProtocol() {
+       ShapeMarkFactory smf = new ShapeMarkFactory();
+       try {
+           this.exp = ff
+                   .literal("xxx://POLYGON((0.25 0.25, 0.5 0.75, 0.75 
0.25,))");
+           if (smf.getShape(null, this.exp, this.feature) == null) {
+               assertTrue(true);
+               return;
+           }
+       } catch (Exception e) {
+           assertTrue(false);
+           return;
+       }
+
+       assertTrue(false);
+    }
+}

Property changes on: 
modules/library/render/src/test/java/org/geotools/renderer/style/ShapeMarkFactoryTest.java
___________________________________________________________________
Added: svn:keywords
    + Id



------------------------------------------------------------------------------
_______________________________________________
Geoserver-devel mailing list
[email protected]
https://lists.sourceforge.net/lists/listinfo/geoserver-devel

Reply via email to