Changeset 918

Show
Ignore:
Timestamp:
08/12/08 16:19:36 (3 months ago)
Author:
pvalsecc
Message:

Ticket #206: Add GeoJSON decoding

Files:

Legend:

Unmodified
Added
Removed
Modified
Copied
Moved
  • sandbox/camptocamp/MapFishUnhcr/server/java/mapfish-geo-lib/pom.xml

    r916 r918  
    106106    </dependency> 
    107107    <dependency> 
    108       <groupId>net.sf.json-lib</groupId> 
    109       <artifactId>json-lib</artifactId> 
    110       <version>2.2</version> 
    111       <classifier>jdk15</classifier>  
     108      <groupId>org.json</groupId> 
     109      <artifactId>json</artifactId> 
     110      <version>20080701</version> 
    112111    </dependency> 
    113112    <dependency> 
  • sandbox/camptocamp/MapFishUnhcr/server/java/mapfish-geo-lib/src/main/java/org/mapfish/geo/MfFeature.java

    r279 r918  
    1818 */ 
    1919 
    20 /* 
    21  * MfFeature.java 
    22  * 
    23  * Created on January 21, 2008, 8:43 PM 
    24  * 
    25  * To change this template, choose Tools | Template Manager 
    26  * and open the template in the editor. 
    27  */ 
    28  
    2920package org.mapfish.geo; 
    3021 
    31 import net.sf.json.util.JSONBuilder; 
     22import org.json.JSONWriter; 
     23import org.json.JSONException; 
    3224 
    3325/** 
     
    3628 */ 
    3729public abstract class MfFeature implements MfGeo { 
    38     private final GeoType geoType; 
    39      
    4030    /** 
    4131     * Creates a new instance of MfFeature 
    4232     */ 
    4333    protected MfFeature() { 
    44         geoType = GeoType.FEATURE; 
    4534    } 
    4635     
    4736    public GeoType getGeoType() { 
    48         return geoType
     37        return GeoType.FEATURE
    4938    } 
    5039     
     
    5746     * Add the "key" and "value" pairs to the provided builder. 
    5847     */ 
    59     public abstract void toJSON(JSONBuilder builder)
     48    public abstract void toJSON(JSONWriter builder) throws JSONException
    6049} 
  • sandbox/camptocamp/MapFishUnhcr/server/java/mapfish-geo-lib/src/main/java/org/mapfish/geo/MfFeatureCollection.java

    r279 r918  
    1818 */ 
    1919 
    20 /* 
    21  * MfFeatureCollection.java 
    22  * 
    23  * Created on January 21, 2008, 8:56 PM 
    24  * 
    25  * To change this template, choose Tools | Template Manager 
    26  * and open the template in the editor. 
    27  */ 
    28  
    2920package org.mapfish.geo; 
    3021 
     
    3627 */ 
    3728public class MfFeatureCollection implements MfGeo { 
    38     private final GeoType geoType; 
    3929    private final Collection<MfFeature> collection; 
    4030 
     
    4333     */ 
    4434    public MfFeatureCollection(Collection<MfFeature> collection) { 
    45         this.geoType = GeoType.FEATURECOLLECTION; 
    4635        this.collection = collection; 
    4736    } 
    4837        
    4938    public GeoType getGeoType() { 
    50         return geoType
     39        return GeoType.FEATURECOLLECTION
    5140    } 
    5241     
  • sandbox/camptocamp/MapFishUnhcr/server/java/mapfish-geo-lib/src/main/java/org/mapfish/geo/MfGeo.java

    r279 r918  
    1818 */ 
    1919 
    20 /* 
    21  * MfGeo.java 
    22  * 
    23  * Created on January 21, 2008, 8:02 PM 
    24  * 
    25  * To change this template, choose Tools | Template Manager 
    26  * and open the template in the editor. 
    27  */ 
    28  
    2920package org.mapfish.geo; 
    3021 
    3122/** 
     23 * Base class for GeoJSON objects 
    3224 * 
    3325 * @author Eric Lemoine, Camptocamp. 
  • sandbox/camptocamp/MapFishUnhcr/server/java/mapfish-geo-lib/src/main/java/org/mapfish/geo/MfGeoJSONWriter.java

    r424 r918  
    4040import com.vividsolutions.jts.geom.Point; 
    4141import com.vividsolutions.jts.geom.Polygon; 
    42 import net.sf.json.JSONException; 
    43 import net.sf.json.util.JSONBuilder; 
     42import org.json.JSONException; 
     43import org.json.JSONWriter; 
    4444 
    4545import java.util.Iterator; 
     
    4747 
    4848/** 
    49  * This class is coded against the draft 5 version of the spec 
     49 * This class is coded against the version 1.0 of the spec 
    5050 * on http://geojson.org. 
    5151 * 
     
    5959 * 
    6060 */ 
    61 public class MfGeoJSON
    62     private final JSONBuilder builder; 
    63      
    64     public MfGeoJSON(JSONBuilder builder) { 
     61public class MfGeoJSONWriter
     62    private final JSONWriter builder; 
     63 
     64    public MfGeoJSONWriter(JSONWriter builder) { 
    6565        this.builder = builder; 
    6666    } 
    67      
    68     public void encode(MfGeo o)
     67 
     68    public void encode(MfGeo o) throws JSONException
    6969        switch (o.getGeoType()) { 
    7070            case FEATURE: 
     
    8484        } 
    8585    } 
    86      
     86 
    8787    public void encodeFeatureCollection(MfFeatureCollection c) throws JSONException { 
    8888        builder.object(); 
     
    9090        builder.key("features"); 
    9191        builder.array(); 
    92          
     92 
    9393        Iterator<MfFeature> i = c.getCollection().iterator(); 
    9494        while (i.hasNext()) { 
     
    9696            encodeFeature(f); 
    9797        } 
    98          
    99         builder.endArray(); 
    100         builder.endObject(); 
    101     } 
    102      
     98 
     99        builder.endArray(); 
     100        builder.endObject(); 
     101    } 
     102 
    103103    public void encodeFeature(MfFeature f) throws JSONException { 
    104104        builder.object(); 
     
    106106        builder.key("id").value(f.getFeatureId()); 
    107107        builder.key("geometry"); 
    108          
     108 
    109109        Geometry g; 
    110110        MfGeometry mfg; 
     
    115115            builder.value(null); 
    116116        } 
    117          
     117 
    118118        builder.key("properties"); 
    119119        builder.object(); 
     
    171171            encodeGeomCollection((GeometryCollection) g); 
    172172        } 
    173          
    174         builder.endObject(); 
    175     } 
    176  
    177     private void encodeGeomCollection(GeometryCollection collection)
     173 
     174        builder.endObject(); 
     175    } 
     176 
     177    private void encodeGeomCollection(GeometryCollection collection) throws JSONException
    178178        builder.array(); 
    179179        builder.key("geometries"); 
     
    203203    } 
    204204 
    205     private void encodeCoordinate(Coordinate coord)
     205    private void encodeCoordinate(Coordinate coord) throws JSONException
    206206        builder.array(); 
    207207        builder.value(coord.x); 
     
    214214     * @param env envelope representing bounding box 
    215215     */ 
    216     protected void encodeBoundingBox(Envelope env)
     216    protected void encodeBoundingBox(Envelope env) throws JSONException
    217217        builder.key("bbox"); 
    218218        builder.array(); 
  • sandbox/camptocamp/MapFishUnhcr/server/java/mapfish-geo-lib/src/main/java/org/mapfish/geo/MfGeometry.java

    r279 r918  
    1818 */ 
    1919 
    20 /* 
    21  * MfGeometry.java 
    22  * 
    23  * Created on January 21, 2008, 8:13 PM 
    24  * 
    25  * To change this template, choose Tools | Template Manager 
    26  * and open the template in the editor. 
    27  */ 
    28  
    2920package org.mapfish.geo; 
    3021 
     
    3627 */ 
    3728public class MfGeometry implements MfGeo { 
    38     private final GeoType geoType; 
    3929    private final Geometry jtsGeometry; 
    4030     
     
    4333     */ 
    4434    public MfGeometry(Geometry jtsGeometry) { 
    45         this.geoType = GeoType.GEOMETRY; 
    4635        this.jtsGeometry = jtsGeometry; 
    4736    } 
    4837     
    4938    public GeoType getGeoType() { 
    50         return geoType
     39        return GeoType.GEOMETRY
    5140    } 
    5241     
  • sandbox/camptocamp/MapFishUnhcr/server/java/mapfish-geo-lib/src/test/java/org/mapfish/geo/MfFeatureCollectionTest.java

    r279 r918  
     1/* 
     2 * Copyright (C) 2008  Camptocamp 
     3 * 
     4 * This file is part of MapFish 
     5 * 
     6 * MapFish is free software: you can redistribute it and/or modify 
     7 * it under the terms of the GNU Lesser General Public License as published by 
     8 * the Free Software Foundation, either version 3 of the License, or 
     9 * (at your option) any later version. 
     10 * 
     11 * MapFish is distributed in the hope that it will be useful, 
     12 * but WITHOUT ANY WARRANTY; without even the implied warranty of 
     13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the 
     14 * GNU Lesser General Public License for more details. 
     15 * 
     16 * You should have received a copy of the GNU Lesser General Public License 
     17 * along with MapFish.  If not, see <http://www.gnu.org/licenses/>. 
     18 */ 
     19 
    120package org.mapfish.geo; 
    221 
     22import junit.framework.TestCase; 
     23 
    324import java.util.LinkedList; 
    4  
    5 import junit.framework.Test; 
    6 import junit.framework.TestCase; 
    7 import junit.framework.TestSuite; 
    8  
    9 import net.sf.json.util.JSONBuilder; 
    1025 
    1126/** 
  • sandbox/camptocamp/MapFishUnhcr/server/java/mapfish-geo-lib/src/test/java/org/mapfish/geo/MfFeatureTest.java

    r279 r918  
     1/* 
     2 * Copyright (C) 2008  Camptocamp 
     3 * 
     4 * This file is part of MapFish 
     5 * 
     6 * MapFish is free software: you can redistribute it and/or modify 
     7 * it under the terms of the GNU Lesser General Public License as published by 
     8 * the Free Software Foundation, either version 3 of the License, or 
     9 * (at your option) any later version. 
     10 * 
     11 * MapFish is distributed in the hope that it will be useful, 
     12 * but WITHOUT ANY WARRANTY; without even the implied warranty of 
     13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the 
     14 * GNU Lesser General Public License for more details. 
     15 * 
     16 * You should have received a copy of the GNU Lesser General Public License 
     17 * along with MapFish.  If not, see <http://www.gnu.org/licenses/>. 
     18 */ 
     19 
    120package org.mapfish.geo; 
    221 
    3 import junit.framework.Test; 
    422import junit.framework.TestCase; 
    5 import junit.framework.TestSuite; 
    623 
    7 import net.sf.json.util.JSONBuilder; 
     24import org.json.JSONWriter; 
    825 
    926/** 
     
    2845            public String getFeatureId() { return null; } 
    2946            public MfGeometry getMfGeometry() { return null; } 
    30             public void toJSON(JSONBuilder builder) {} 
     47            public void toJSON(JSONWriter builder) {} 
    3148 
    3249        }; 
  • sandbox/camptocamp/MapFishUnhcr/server/java/mapfish-geo-lib/src/test/java/org/mapfish/geo/MfGeoJSONWriterTest.java

    r424 r918  
     1/* 
     2 * Copyright (C) 2008  Camptocamp 
     3 * 
     4 * This file is part of MapFish 
     5 * 
     6 * MapFish is free software: you can redistribute it and/or modify 
     7 * it under the terms of the GNU Lesser General Public License as published by 
     8 * the Free Software Foundation, either version 3 of the License, or 
     9 * (at your option) any later version. 
     10 * 
     11 * MapFish is distributed in the hope that it will be useful, 
     12 * but WITHOUT ANY WARRANTY; without even the implied warranty of 
     13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the 
     14 * GNU Lesser General Public License for more details. 
     15 * 
     16 * You should have received a copy of the GNU Lesser General Public License 
     17 * along with MapFish.  If not, see <http://www.gnu.org/licenses/>. 
     18 */ 
     19 
    120package org.mapfish.geo; 
    221 
     22import com.vividsolutions.jts.geom.Coordinate; 
     23import com.vividsolutions.jts.geom.Geometry; 
     24import com.vividsolutions.jts.geom.GeometryFactory; 
     25import com.vividsolutions.jts.geom.LinearRing; 
     26import junit.framework.TestCase; 
     27import org.json.JSONStringer; 
     28import org.json.JSONWriter; 
     29import org.json.JSONException; 
     30 
    331import java.util.LinkedList; 
    4  
    5 import junit.framework.Test; 
    6 import junit.framework.TestCase; 
    7 import junit.framework.TestSuite; 
    8  
    9 import net.sf.json.util.JSONBuilder; 
    10 import net.sf.json.util.JSONStringer; 
    11  
    12 import com.vividsolutions.jts.geom.Geometry; 
    13 import com.vividsolutions.jts.geom.LinearRing; 
    14 import com.vividsolutions.jts.geom.GeometryFactory; 
    15 import com.vividsolutions.jts.geom.Coordinate; 
    1632 
    1733/** 
    1834 * Unit test for MfGeoJSON. 
    1935 */ 
    20 public class MfGeoJSONTest extends TestCase 
     36public class MfGeoJSONWriterTest extends TestCase 
    2137{ 
    2238    /** 
     
    2541     * @param testName name of the test case 
    2642     */ 
    27     public MfGeoJSONTest(String testName) { 
     43    public MfGeoJSONWriterTest(String testName) { 
    2844        super(testName); 
    2945    } 
     
    3248     * Test. 
    3349     */ 
    34     public void testMfGeoJSON()
     50    public void testMfGeoJSON() throws JSONException
    3551        JSONStringer stringer; 
    36         MfGeoJSON builder; 
     52        MfGeoJSONWriter builder; 
    3753 
    3854        Geometry g; 
     
    5167        // encodeGeometry POINT test 
    5268        stringer = new JSONStringer(); 
    53         builder = new MfGeoJSON(stringer); 
     69        builder = new MfGeoJSONWriter(stringer); 
    5470        g = new GeometryFactory().createPoint(coord1); 
    5571        geojsonExpected = 
     
    6177        // encodeGeometry LINESTRING test 
    6278        stringer = new JSONStringer(); 
    63         builder = new MfGeoJSON(stringer); 
     79        builder = new MfGeoJSONWriter(stringer); 
    6480        Coordinate[] coordArrayLS = {coord1, coord2}; 
    6581        g = new GeometryFactory().createLineString(coordArrayLS); 
     
    7288        // encodeGeometry POLYGON test 
    7389        stringer = new JSONStringer(); 
    74         builder = new MfGeoJSON(stringer); 
     90        builder = new MfGeoJSONWriter(stringer); 
    7591        Coordinate[] coordArrayLR = {coord1, coord2, coord3, coord1}; 
    7692        LinearRing lr = new GeometryFactory().createLinearRing(coordArrayLR); 
     
    84100        // encodeFeature test 
    85101        stringer = new JSONStringer(); 
    86         builder = new MfGeoJSON(stringer); 
     102        builder = new MfGeoJSONWriter(stringer); 
    87103        f1 = new MfFeature() { 
    88104            public String getFeatureId() { 
     
    93109                    new GeometryFactory().createPoint(coord1)); 
    94110            } 
    95             public void toJSON(JSONBuilder builder)
     111            public void toJSON(JSONWriter builder) throws JSONException
    96112                builder.key("prop_foo").value("foo"); 
    97113            } 
     
    105121        // encodeFeature test with null internal geometry 
    106122        stringer = new JSONStringer(); 
    107         builder = new MfGeoJSON(stringer); 
     123        builder = new MfGeoJSONWriter(stringer); 
    108124        f1 = new MfFeature() { 
    109125            public String getFeatureId() { 
     
    113129                return new MfGeometry(null); 
    114130            } 
    115             public void toJSON(JSONBuilder builder)
     131            public void toJSON(JSONWriter builder) throws JSONException
    116132                builder.key("prop_foo").value("foo"); 
    117133            } 
     
    125141        // encodeFeature test with null geometry 
    126142        stringer = new JSONStringer(); 
    127         builder = new MfGeoJSON(stringer); 
     143        builder = new MfGeoJSONWriter(stringer); 
    128144        f1 = new MfFeature() { 
    129145            public String getFeatureId() { 
     
    133149                return null; 
    134150            } 
    135             public void toJSON(JSONBuilder builder)
     151            public void toJSON(JSONWriter builder) throws JSONException
    136152                builder.key("prop_foo").value("foo"); 
    137153            } 
     
    144160        // encodeFeatureCollection test 
    145161        stringer = new JSONStringer(); 
    146         builder = new MfGeoJSON(stringer); 
     162        builder = new MfGeoJSONWriter(stringer); 
    147163        f1 = new MfFeature() { 
    148164            public String getFeatureId() { 
     
    153169                    new GeometryFactory().createPoint(coord1)); 
    154170            } 
    155             public void toJSON(JSONBuilder builder)
     171            public void toJSON(JSONWriter builder) throws JSONException
    156172                builder.key("prop_foo").value("foo"); 
    157173            } 
     
    165181                    new GeometryFactory().createPoint(coord2)); 
    166182            } 
    167             public void toJSON(JSONBuilder builder)
     183            public void toJSON(JSONWriter builder) throws JSONException
    168184                builder.key("prop_bar").value("bar"); 
    169185            } 
  • sandbox/camptocamp/MapFishUnhcr/server/java/mapfish-geo-lib/src/test/java/org/mapfish/geo/MfGeometryTest.java

    r279 r918  
     1/* 
     2 * Copyright (C) 2008  Camptocamp 
     3 * 
     4 * This file is part of MapFish 
     5 * 
     6 * MapFish is free software: you can redistribute it and/or modify 
     7 * it under the terms of the GNU Lesser General Public License as published by 
     8 * the Free Software Foundation, either version 3 of the License, or 
     9 * (at your option) any later version. 
     10 * 
     11 * MapFish is distributed in the hope that it will be useful, 
     12 * but WITHOUT ANY WARRANTY; without even the implied warranty of 
     13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the 
     14 * GNU Lesser General Public License for more details. 
     15 * 
     16 * You should have received a copy of the GNU Lesser General Public License 
     17 * along with MapFish.  If not, see <http://www.gnu.org/licenses/>. 
     18 */ 
     19 
    120package org.mapfish.geo; 
    221 
    3 import java.util.LinkedList; 
    4  
    5 import junit.framework.Test; 
    6 import junit.framework.TestCase; 
    7 import junit.framework.TestSuite; 
    8  
     22import com.vividsolutions.jts.geom.Coordinate; 
    923import com.vividsolutions.jts.geom.Geometry; 
    1024import com.vividsolutions.jts.geom.GeometryFactory; 
    11 import com.vividsolutions.jts.geom.Coordinate; 
     25import junit.framework.TestCase; 
    1226 
    1327/** 
  • sandbox/camptocamp/MapFishUnhcr/server/java/print/print-lib/pom.xml

    r911 r918  
    7474      <groupId>org.json</groupId> 
    7575      <artifactId>json</artifactId> 
    76       <version>20070829</version> 
     76      <version>20080701</version> 
    7777    </dependency> 
    7878    <dependency>