geoserver中除了使用kml来查询数据以外,还可以使用csql或ecsql

时间:2021-11-05 08:36:10
  1. package com.geoserver;
  2. import java.io.IOException;
  3. import java.util.ArrayList;
  4. import java.util.HashMap;
  5. import java.util.Map;
  6. import org.geotools.data.DataStore;
  7. import org.geotools.data.DataStoreFinder;
  8. import org.geotools.data.simple.SimpleFeatureCollection;
  9. import org.geotools.data.simple.SimpleFeatureSource;
  10. import org.geotools.feature.FeatureIterator;
  11. import org.geotools.filter.text.cql2.CQL;
  12. import org.geotools.filter.text.cql2.CQLException;
  13. import org.geotools.geometry.jts.ReferencedEnvelope;
  14. import org.opengis.feature.simple.SimpleFeature;
  15. import org.opengis.filter.Filter;
  16. /**
  17. * 采用geotools中公共查询语言
  18. * 过滤条件如下
  19. *
  20. * 例如:
  21. *    PERSONS > 15000000
  22. *    PERSONS BETWEEN 1000000 AND 3000000
  23. *    STATE_NAME LIKE 'N%'
  24. *    STATE_NAME = 'California'
  25. *    MALE > FEMALE
  26. *    UNEMPLOY / (EMPLOYED + UNEMPLOY) > 0.07
  27. *     IN ('states.1', 'states.12'):
  28. *   STATE_NAME IN ('New York', 'California', 'Montana', 'Texas'):
  29. *  带函数的使用:
  30. *     strToLowerCase(STATE_NAME) like ‘%m%’
  31. *
  32. *
  33. *
  34. * @Title:
  35. * @Description: 实现TODO
  36. * @Copyright:Copyright (c) 2011
  37. * @Company:
  38. * @Date:2012-9-10
  39. * @author  longgangbai
  40. * @version 1.0
  41. */
  42. public class GeoServerCQLECQL {
  43. /**
  44. *
  45. * @param filterStr
  46. * @param layerName
  47. * @return
  48. * @throws IOException
  49. */
  50. public static ArrayList<SimpleFeature> queryMethod(String filterStr,String layerName) throws IOException {
  51. String getCapabilities = "http://localhost:8080/geoserver/wfs?REQUEST=GetCapabilities";
  52. Map<String,String> connectionParameters = new HashMap<String,String>();
  53. connectionParameters.put("WFSDataStoreFactory:GET_CAPABILITIES_URL", getCapabilities );
  54. // Step 2 - connection
  55. DataStore data = DataStoreFinder.getDataStore( connectionParameters );
  56. SimpleFeatureSource featureSource =data.getFeatureSource(layerName);
  57. ArrayList<SimpleFeature> featureList = new ArrayList<SimpleFeature>();
  58. if(featureSource==null)
  59. return featureList;
  60. try {
  61. Filter  filter = CQL.toFilter(filterStr); // filterStr形式 如  name='武汉大学' or code like 'tt123%'
  62. SimpleFeatureCollection result = featureSource.getFeatures(filter);
  63. ReferencedEnvelope bounds = new ReferencedEnvelope();
  64. FeatureIterator<SimpleFeature> itertor = result.features();
  65. while (itertor.hasNext()) {
  66. SimpleFeature feature = itertor.next();
  67. bounds.include( feature.getBounds() );
  68. featureList.add(feature);
  69. }
  70. System.out.println( "Calculated Bounds:"+ bounds );
  71. itertor.close();
  72. result.close( itertor );
  73. return featureList;
  74. } catch (CQLException e) {
  75. // TODO Auto-generated catch block
  76. e.printStackTrace();
  77. } catch (IOException e) {
  78. // TODO Auto-generated catch block
  79. e.printStackTrace();
  80. }finally {
  81. }
  82. return null;
  83. }
  84. public static void main(String[] args) throws IOException {
  85. ArrayList<SimpleFeature> list=queryMethod("STATE_NAME='Arizona'","topp:states");
  86. System.out.println("list="+list.toString());
  87. }
  88. }