PageRenderTime 29ms CodeModel.GetById 2ms app.highlight 21ms RepoModel.GetById 1ms app.codeStats 1ms

/webportal/src/main/java/au/org/emii/portal/util/GeoJSONUtilitiesImpl.java

http://alageospatialportal.googlecode.com/
Java | 185 lines | 132 code | 37 blank | 16 comment | 12 complexity | 45f219361c34981a379dc9c15f3ae154 MD5 | raw file
  1/*
  2 * Implementation of GeoJSONUtilities
  3 * 
  4 */
  5package au.org.emii.portal.util;
  6
  7import au.org.emii.portal.net.HttpConnection;
  8import java.io.IOException;
  9import java.io.InputStream;
 10import java.net.URLConnection;
 11import java.util.HashMap;
 12import net.sf.json.JSONObject;
 13import org.apache.commons.io.IOUtils;
 14import org.apache.log4j.Logger;
 15import org.springframework.beans.factory.annotation.Autowired;
 16
 17/**
 18 *
 19 * @author brendon
 20 * the feature checking comes from Nicholas Bergson-Shilcock, The Open Planning Project
 21 * and the GeoJSONParser code he wrote for GeoTOOLS
 22 *
 23 */
 24public class GeoJSONUtilitiesImpl implements GeoJSONUtilities {
 25
 26    private static HashMap typeMap;
 27    protected Logger logger = Logger.getLogger(this.getClass());
 28    private HttpConnection httpConnection = null;
 29
 30    @Override
 31    public HttpConnection getHttpConnection() {
 32        return httpConnection;
 33    }
 34
 35    @Autowired
 36    @Override
 37    public void setHttpConnection(HttpConnection httpConnection) {
 38        this.httpConnection = httpConnection;
 39    }
 40
 41    @Override
 42    public String getJson(String url) {
 43        InputStream in = null;
 44        String json = null;
 45        URLConnection connection = null;
 46
 47        try {
 48
 49            connection = httpConnection.configureSlowURLConnection(url);
 50            connection.addRequestProperty("accept", "application/json, text/javascript, */*"); 
 51            in = connection.getInputStream();
 52            json = IOUtils.toString(in);
 53            //json = cleanUpJson(json);
 54            return json;
 55        } catch (IOException iox) {
 56            logger.debug(iox.toString());
 57            
 58        }
 59        return "fail";
 60
 61    }
 62
 63    @Override
 64    public int getFirstFeatureType(JSONObject obj) {
 65        JSONObject prototype = null;
 66        int objType = type(obj.getString("type"));
 67
 68        switch (objType) {
 69            case FEATURE:
 70                prototype = obj;
 71                JSONObject objGeom = prototype.getJSONObject("geometry");
 72                objType = type(objGeom.getString("type"));
 73
 74                break;
 75
 76            case FEATURECOLLECTION:
 77
 78                if (!obj.containsKey("features")) {
 79                    logger.debug("no features in this geoJSON object");
 80                }
 81
 82                try {
 83                    prototype = obj.getJSONArray("features").getJSONObject(0);
 84                    //get the first fe1ature and check that
 85                    JSONObject objGeom1 = prototype.getJSONObject("geometry");
 86                    objType = type(objGeom1.getString("type"));
 87
 88                } catch (IndexOutOfBoundsException ioex) {
 89                    //no mappable features found
 90                    objType = -1;
 91                }
 92
 93                break;
 94
 95            default:
 96                logger.debug("Object must be feature or feature collection");
 97        }
 98
 99        return objType;
100    }
101
102    @Override
103    public String getFirstFeatureValue(JSONObject obj, String key) {
104        JSONObject prototype = null;
105        int objType = type(obj.getString("type"));
106        String value = "";
107
108        switch (objType) {
109            case FEATURE:
110                prototype = obj;
111                JSONObject objProps = prototype.getJSONObject("properties");
112                if (objProps.containsKey(key)) {
113                    value = objProps.getString(key);
114                }
115
116                break;
117
118            case FEATURECOLLECTION:
119
120                if (!obj.containsKey("features")) {
121                    logger.debug("no features in this geoJSON object");
122                }
123
124                try {
125                    int countFeatures = obj.getJSONArray("features").size();
126                    logger.debug("Iterating thru' " + countFeatures + " features");
127                    for (int i = 0; i < countFeatures; i++) {
128                        JSONObject o = obj.getJSONArray("features").getJSONObject(i);
129                        JSONObject oprop;
130                        try {
131                            oprop = o.getJSONObject("properties");
132
133                        } catch (Exception e) {
134                            oprop = o.getJSONArray("properties").getJSONObject(0);
135                        }
136                        
137                        //System.out.println("key value: " + oprop.getString(key));
138                        if (oprop.containsKey(key)) {
139                            value = oprop.getString(key);
140                            if (!value.trim().equalsIgnoreCase("")) {
141                                break;
142                            }
143                        }
144                    }
145
146                } catch (IndexOutOfBoundsException ioex) {
147                    //no mappable features found
148                    value = "";
149                }
150
151                break;
152
153            default:
154                logger.debug("Object must be feature or feature collection");
155        }
156
157        System.out.println("geojson.value: " + value); 
158
159        return value;
160    }
161
162    public int type(String type) {
163        if (typeMap == null) {
164            typeMap = new HashMap();
165            typeMap.put("feature", Integer.valueOf(FEATURE));
166            typeMap.put("featurecollection", Integer.valueOf(FEATURECOLLECTION));
167            typeMap.put("point", Integer.valueOf(POINT));
168            typeMap.put("linestring", Integer.valueOf(LINESTRING));
169            typeMap.put("polygon", Integer.valueOf(POLYGON));
170            typeMap.put("multipoint", Integer.valueOf(MULTIPOINT));
171            typeMap.put("multilinestring", Integer.valueOf(MULTILINESTRING));
172            typeMap.put("multipolygon", Integer.valueOf(MULTIPOLYGON));
173            typeMap.put("geometrycollection",
174                    Integer.valueOf(GEOMETRYCOLLECTION));
175        }
176
177        Integer val = (Integer) typeMap.get(type.toLowerCase());
178
179        if (val == null) {
180            logger.debug("Unknown object type '" + type + "'");
181        }
182
183        return val.intValue();
184    }
185}