View Javadoc
1   /*
2    * Copyright (c) 2002-2015, Mairie de Paris
3    * All rights reserved.
4    *
5    * Redistribution and use in source and binary forms, with or without
6    * modification, are permitted provided that the following conditions
7    * are met:
8    *
9    *  1. Redistributions of source code must retain the above copyright notice
10   *     and the following disclaimer.
11   *
12   *  2. Redistributions in binary form must reproduce the above copyright notice
13   *     and the following disclaimer in the documentation and/or other materials
14   *     provided with the distribution.
15   *
16   *  3. Neither the name of 'Mairie de Paris' nor 'Lutece' nor the names of its
17   *     contributors may be used to endorse or promote products derived from
18   *     this software without specific prior written permission.
19   *
20   * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
21   * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
22   * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
23   * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDERS OR CONTRIBUTORS BE
24   * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
25   * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
26   * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
27   * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
28   * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
29   * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
30   * POSSIBILITY OF SUCH DAMAGE.
31   *
32   * License 1.0
33   */
34  package fr.paris.lutece.plugins.leaflet.business;
35  
36  import fr.paris.lutece.util.xml.XmlUtil;
37  
38  /*
39  import org.codehaus.jackson.JsonParseException;
40  import org.codehaus.jackson.annotate.JsonAutoDetect;
41  import org.codehaus.jackson.annotate.JsonAutoDetect.Visibility;
42  import org.codehaus.jackson.annotate.JsonProperty;
43  import org.codehaus.jackson.map.DeserializationConfig;
44  import org.codehaus.jackson.map.JsonMappingException;
45  import org.codehaus.jackson.map.ObjectMapper;
46  */
47  
48  import com.fasterxml.jackson.annotation.JsonAutoDetect;
49  import com.fasterxml.jackson.annotation.JsonAutoDetect.Visibility;
50  import com.fasterxml.jackson.annotation.JsonProperty;
51  import com.fasterxml.jackson.core.JsonParseException;
52  import com.fasterxml.jackson.databind.JsonMappingException;
53  import com.fasterxml.jackson.databind.ObjectMapper;
54  import com.fasterxml.jackson.databind.DeserializationFeature;
55  
56  import java.io.IOException;
57  
58  import java.util.HashMap;
59  import java.util.List;
60  import java.util.Map;
61  
62  
63  /**
64   * A geoloc item with methods to translate to/from geojson and to XML
65   */
66  @JsonAutoDetect( creatorVisibility = Visibility.NONE, fieldVisibility = Visibility.NONE, getterVisibility = Visibility.NONE, isGetterVisibility = Visibility.NONE, setterVisibility = Visibility.NONE )
67  public class GeolocItemPolygon
68  {
69      public static final String PATH_TYPE = "type";
70      public static final String PATH_GEOMETRY = "geometry";
71      public static final String PATH_GEOMETRY_TYPE = "type";
72      public static final String PATH_GEOMETRY_COORDINATES = "coordinates";
73      public static final String PATH_PROPERTIES = "properties";
74      public static final String PATH_PROPERTIES_ICON = "icon";
75      public static final String PATH_PROPERTIES_LAYER = "layer";
76      public static final String XML_ROOT = "geoloc";
77      public static final String XML_LON = "lon";
78      public static final String XML_LAT = "lat";
79      public static final String XML_ICON = "icon";
80      public static final String XML_LAYER = "layer";
81      public static final String VALUE_TYPE = "Feature";
82      public static final String VALUE_GEOMETRY_TYPE_POLYGON = "Polygon";
83      public static final String VALUE_GEOMETRY_TYPE_POLYLINE = "Polyline";
84      private static final ObjectMapper _objectMapper;
85  
86      static
87      {
88          _objectMapper = new ObjectMapper(  );
89          //_objectMapper.configure( DeserializationConfig.Feature.FAIL_ON_UNKNOWN_PROPERTIES, false );
90          _objectMapper.configure( DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false );
91      }
92  
93      private List<Double> _lonlat;
94      private List<List<Double>> _polygonLonLoat;
95      private String _icon;
96      private String _layer;
97      private String _typegeometry;
98      private String _typePolygon;
99  
100     //Example
101     //{"type":"Feature","geometry":{"type":"Point","coordinates":[2.31272,48.83632]},"properties":{"layer":"aco"}}
102 
103     /**
104      * Sets the Geometry
105      *
106      * @param geometry the geometry
107      */
108     @JsonProperty( PATH_GEOMETRY )
109     public void setGeometry( Map<String, Object> geometry )
110     {
111         //_lonlat = (List<Double>) geometry.get( PATH_GEOMETRY_COORDINATES );
112         //_polygonLonLoat.add(_lonlat);
113     	_polygonLonLoat = ( List<List<Double>> ) geometry.get( PATH_GEOMETRY_COORDINATES );
114     }
115 
116     /**
117      * Sets the Properties
118      *
119      * @param properties the geometry
120      */
121     @JsonProperty( PATH_PROPERTIES )
122     public void setProperties( Map<String, Object> properties )
123     {
124         _icon = (String) properties.get( PATH_PROPERTIES_ICON );
125         _layer = (String) properties.get( PATH_PROPERTIES_LAYER );
126     }
127 
128     /**
129      * Returns the Type
130      *
131      * @return The type
132      */
133     @JsonProperty( PATH_TYPE )
134     public String getType(  )
135     {
136         return VALUE_TYPE;
137     }
138 
139     /**
140      * Returns the Properties
141      *
142      * @return The properties
143      */
144     @JsonProperty( PATH_PROPERTIES )
145     public Map<String, Object> getProperties(  )
146     {
147         HashMap<String, Object> properties = new HashMap<String, Object>(  );
148 
149         if ( _icon != null )
150         {
151             properties.put( PATH_PROPERTIES_ICON, _icon );
152         }
153 
154         if ( _layer != null )
155         {
156             properties.put( PATH_PROPERTIES_LAYER, _layer );
157         }
158 
159         return properties;
160     }
161 
162     /**
163      * Returns the Geometry
164      *
165      * @return The geometry
166      */
167     @JsonProperty( PATH_GEOMETRY )
168     public Map<String, Object> getGeometry(  )
169     {
170         HashMap<String, Object> geometry = new HashMap<String, Object>(  );
171         //geometry.put( PATH_GEOMETRY_TYPE, VALUE_GEOMETRY_TYPE_POLYGON );
172         geometry.put( PATH_GEOMETRY_TYPE, _typegeometry );
173         //geometry.put( PATH_GEOMETRY_COORDINATES, _lonlat );
174         geometry.put( PATH_GEOMETRY_COORDINATES, _polygonLonLoat );
175 
176         return geometry;
177     }
178 
179     /**
180      * Returns the LonLat
181      *
182      * @return The LonLat
183      */
184     public List<Double> getLonLat(  )
185     {
186         return _lonlat;
187     }
188 
189     /**
190      * Returns the Lon
191      *
192      * @return The Lon
193      */
194     public double getLon(  )
195     {
196         return _lonlat.get( 0 );
197     }
198 
199     /**
200      * Returns the Lat
201      *
202      * @return The Lat
203      */
204     public double getLat(  )
205     {
206         return _lonlat.get( 1 );
207     }
208 
209     /**
210      * Returns the Icon
211      *
212      * @return The icon
213      */
214     public String getIcon(  )
215     {
216         return _icon;
217     }
218 
219     /**
220      * Returns the Layer
221      *
222      * @return The layer
223      */
224     public String getLayer(  )
225     {
226         return _layer;
227     }
228 
229     /**
230      * Sets the Icon
231      *
232      * @param icon the icon
233      */
234     public void setIcon( String icon )
235     {
236         _icon = icon;
237     }
238 
239     /**
240      * Writes the geojson to a String
241      *
242      * @return The geojseon String
243      */
244     public String toJSON(  )
245     {
246         try
247         {
248             return _objectMapper.writeValueAsString( this );
249         }
250         catch ( IOException e )
251         {
252             // Writing to strings should not produce exceptions
253             throw new RuntimeException( e );
254         }
255     }
256 
257     /**
258      * Writes the xml to a String. This is a non standard representation.
259      *
260      * @return The xml String
261      */
262     public String toXML(  )
263     {
264         StringBuffer stringBuffer = new StringBuffer(  );
265         XmlUtil.beginElement( stringBuffer, XML_ROOT );
266         XmlUtil.addElement( stringBuffer, XML_LON, _lonlat.get( 0 ).toString(  ) );
267         XmlUtil.addElement( stringBuffer, XML_LAT, _lonlat.get( 1 ).toString(  ) );
268 
269         if ( _icon != null )
270         {
271             XmlUtil.addElement( stringBuffer, XML_ICON, _icon );
272         }
273 
274         if ( _layer != null )
275         {
276             XmlUtil.addElement( stringBuffer, XML_LAYER, _layer );
277         }
278 
279         XmlUtil.endElement( stringBuffer, XML_ROOT );
280 
281         return stringBuffer.toString(  );
282     }
283 
284     /**
285      * Parses a geojson string to build a GeolocItem Object.
286      *
287      * @return The geolocItem object
288      */
289     public static GeolocItemPolygon fromJSON( String strJson )
290         throws JsonParseException, JsonMappingException, IOException
291     {
292         return _objectMapper.readValue( strJson, GeolocItemPolygon.class );
293     }
294 
295 	public String getTypegeometry() {
296 		return _typegeometry;
297 	}
298 
299 	public void setTypegeometry(String _typegeometry) {
300 		this._typegeometry = _typegeometry;
301 	}
302 }