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