|
@@ -0,0 +1,219 @@
|
|
|
+package cn.com.taiji.cql.test;
|
|
|
+
|
|
|
+import com.alibaba.fastjson.JSONObject;
|
|
|
+import org.geotools.api.data.DataStore;
|
|
|
+import org.geotools.api.data.DataStoreFinder;
|
|
|
+import org.geotools.api.data.Query;
|
|
|
+import org.geotools.api.data.SimpleFeatureSource;
|
|
|
+import org.geotools.api.feature.simple.SimpleFeature;
|
|
|
+import org.geotools.api.feature.simple.SimpleFeatureType;
|
|
|
+import org.geotools.api.feature.type.AttributeDescriptor;
|
|
|
+import org.geotools.api.filter.Filter;
|
|
|
+import org.geotools.api.filter.FilterFactory;
|
|
|
+import org.geotools.data.jdbc.datasource.DBCPDataSourceFactory;
|
|
|
+import org.geotools.data.jdbc.datasource.DataSourceFinder;
|
|
|
+import org.geotools.data.simple.SimpleFeatureCollection;
|
|
|
+import org.geotools.data.simple.SimpleFeatureIterator;
|
|
|
+import org.geotools.factory.CommonFactoryFinder;
|
|
|
+import org.geotools.filter.text.ecql.ECQL;
|
|
|
+import org.geotools.geometry.jts.JTS;
|
|
|
+import org.geotools.jdbc.JDBCDataStoreFactory;
|
|
|
+import org.locationtech.jts.geom.Coordinate;
|
|
|
+import org.locationtech.jts.geom.GeometryFactory;
|
|
|
+import org.locationtech.jts.geom.Point;
|
|
|
+
|
|
|
+import javax.sql.DataSource;
|
|
|
+import java.util.ArrayList;
|
|
|
+import java.util.HashMap;
|
|
|
+import java.util.List;
|
|
|
+import java.util.Map;
|
|
|
+
|
|
|
+public class EcqlTest4 {
|
|
|
+
|
|
|
+ public static void main(String[] args) throws Exception{
|
|
|
+
|
|
|
+ Map<String, Object> mapParams = new HashMap<String, Object>();
|
|
|
+
|
|
|
+ mapParams.put(JDBCDataStoreFactory.DATABASE.key, "geo");
|
|
|
+ mapParams.put(JDBCDataStoreFactory.DBTYPE.key, "mysql");
|
|
|
+
|
|
|
+ mapParams.put(JDBCDataStoreFactory.HOST.key, "172.16.67.23");
|
|
|
+ mapParams.put(JDBCDataStoreFactory.PORT.key, "7306");
|
|
|
+ mapParams.put(JDBCDataStoreFactory.USER.key, "root");
|
|
|
+ mapParams.put(JDBCDataStoreFactory.PASSWD.key, "Ucsp123#");
|
|
|
+
|
|
|
+ queryDistance(mapParams,110.004351,18.518221,2d);
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+ }
|
|
|
+
|
|
|
+
|
|
|
+ public static void queryDistance(Map<String, Object> mapParams,double longitude,double latitude,double radius){
|
|
|
+
|
|
|
+ DataStore dataStore = null;
|
|
|
+
|
|
|
+ String host = mapParams.get(JDBCDataStoreFactory.HOST.key).toString();
|
|
|
+
|
|
|
+ String database = mapParams.get(JDBCDataStoreFactory.DATABASE.key).toString();
|
|
|
+
|
|
|
+ List<Map<String, Object>> dataList = new ArrayList<Map<String, Object>>();
|
|
|
+
|
|
|
+
|
|
|
+ try {
|
|
|
+
|
|
|
+ dataStore = DataStoreFinder.getDataStore(mapParams);
|
|
|
+
|
|
|
+ if (dataStore != null) {
|
|
|
+
|
|
|
+ System.out.println("系统连接到位于:" + host + "的空间数据库" + database + "成功!");
|
|
|
+
|
|
|
+ double pointDistance = radius * Math.sqrt(2);
|
|
|
+
|
|
|
+
|
|
|
+ Point point = new GeometryFactory().createPoint(new Coordinate(longitude, latitude));
|
|
|
+
|
|
|
+ // threshold distance
|
|
|
+ double distance = pointDistance;
|
|
|
+
|
|
|
+ FilterFactory ff = CommonFactoryFinder.getFilterFactory();
|
|
|
+ Filter filter =
|
|
|
+ ff.dwithin(ff.property("location"), ff.literal(point), radius/6378.137, "km");
|
|
|
+
|
|
|
+ //根据表名获取source
|
|
|
+ SimpleFeatureSource fSource=dataStore.getFeatureSource("stg_view_deviceinfo_df");
|
|
|
+
|
|
|
+ SimpleFeatureCollection fCollection =fSource.getFeatures(filter);
|
|
|
+
|
|
|
+ System.out.println("1111111111111111111:"+fCollection.size());
|
|
|
+
|
|
|
+ if(fCollection !=null){
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+ SimpleFeatureIterator itertor = fCollection.features();
|
|
|
+
|
|
|
+ while (itertor.hasNext()){
|
|
|
+ //获取每一个要素
|
|
|
+ SimpleFeature feature = itertor.next();
|
|
|
+
|
|
|
+ Map<String, Object> dataMap = new HashMap<String, Object>();
|
|
|
+
|
|
|
+ SimpleFeatureType simpleFeatureType = feature.getFeatureType();
|
|
|
+
|
|
|
+ List<AttributeDescriptor> attributeList = simpleFeatureType.getAttributeDescriptors();
|
|
|
+ for(AttributeDescriptor ad:attributeList){
|
|
|
+
|
|
|
+ String key_ = ad.getName().toString();
|
|
|
+ Object value_ = feature.getAttribute(key_);
|
|
|
+
|
|
|
+ System.out.println(key_+":"+value_);
|
|
|
+
|
|
|
+ //dataMap.put(key_,value_);
|
|
|
+
|
|
|
+ }
|
|
|
+
|
|
|
+ //dataList.add(dataMap);
|
|
|
+ }
|
|
|
+
|
|
|
+
|
|
|
+ }
|
|
|
+
|
|
|
+
|
|
|
+ } else {
|
|
|
+ System.out.println("系统连接到位于:" + host + "的空间数据库" + database + "失败!请检查相关参数");
|
|
|
+
|
|
|
+ }
|
|
|
+
|
|
|
+ }catch (Exception e) {
|
|
|
+ e.printStackTrace();
|
|
|
+ System.out.println("系统连接到位于:" + mapParams.get(JDBCDataStoreFactory.HOST.key) + "的空间数据库" + mapParams.get(JDBCDataStoreFactory.DATABASE.key) + "失败!请检查相关参数");
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ public static void query1(Map<String, Object> mapParams){
|
|
|
+
|
|
|
+ DataStore dataStore = null;
|
|
|
+
|
|
|
+ String host = mapParams.get(JDBCDataStoreFactory.HOST.key).toString();
|
|
|
+
|
|
|
+ String database = mapParams.get(JDBCDataStoreFactory.DATABASE.key).toString();
|
|
|
+
|
|
|
+
|
|
|
+ try {
|
|
|
+
|
|
|
+ dataStore = DataStoreFinder.getDataStore(mapParams);
|
|
|
+
|
|
|
+ if (dataStore != null) {
|
|
|
+
|
|
|
+ System.out.println("系统连接到位于:" + host + "的空间数据库" + database + "成功!");
|
|
|
+
|
|
|
+ //根据表名获取source
|
|
|
+ SimpleFeatureSource fSource=dataStore.getFeatureSource("geo_test");
|
|
|
+
|
|
|
+ String strData = "name=1";
|
|
|
+
|
|
|
+ Filter filter = ECQL.toFilter(strData);
|
|
|
+
|
|
|
+ SimpleFeatureCollection fCollection =fSource.getFeatures(filter);
|
|
|
+
|
|
|
+ if(fCollection !=null){
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+ SimpleFeatureIterator itertor = fCollection.features();
|
|
|
+
|
|
|
+ while (itertor.hasNext()){
|
|
|
+ //获取每一个要素
|
|
|
+ SimpleFeature feature = itertor.next();
|
|
|
+
|
|
|
+ System.out.println(feature.getAttribute(1));
|
|
|
+ System.out.println(feature.getAttribute("name").toString());
|
|
|
+ }
|
|
|
+
|
|
|
+
|
|
|
+ }
|
|
|
+
|
|
|
+ } else {
|
|
|
+ System.out.println("系统连接到位于:" + host + "的空间数据库" + database + "失败!请检查相关参数");
|
|
|
+
|
|
|
+ }
|
|
|
+
|
|
|
+ }catch (Exception e) {
|
|
|
+ e.printStackTrace();
|
|
|
+ System.out.println("系统连接到位于:" + mapParams.get(JDBCDataStoreFactory.HOST.key) + "的空间数据库" + mapParams.get(JDBCDataStoreFactory.DATABASE.key) + "失败!请检查相关参数");
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+
|
|
|
+ private void test1() throws Exception{
|
|
|
+
|
|
|
+ //先定义cql查询语句
|
|
|
+ String bbox = "bbox (geom, 115.31412 ,10.89577, 125.31412, 80.89577)";
|
|
|
+
|
|
|
+ //声明Query查询对象
|
|
|
+ Query query = new Query("string-data", ECQL.toFilter(bbox));
|
|
|
+
|
|
|
+ Filter filter = ECQL.toFilter(bbox);
|
|
|
+
|
|
|
+ //构建mysql数据库连接数据源--DataSource
|
|
|
+ Map<String, Object> mapParams = new HashMap<String, Object>();
|
|
|
+ mapParams.put(DBCPDataSourceFactory.DSTYPE.key, "DBCP");
|
|
|
+ mapParams.put(DBCPDataSourceFactory.DRIVERCLASS.key, "com.mysql.cj.jdbc.Driver");
|
|
|
+ mapParams.put(DBCPDataSourceFactory.JDBC_URL.key, "jdbc:mysql://172.16.67.23:7306/geo?useUnicode=true&characterEncoding=UTF-8&autoReconnect=true&useSSL=false&zeroDateTimeBehavior=convertToNull&serverTimezone=Asia/Shanghai");
|
|
|
+ mapParams.put(DBCPDataSourceFactory.USERNAME.key, "root");
|
|
|
+ mapParams.put(DBCPDataSourceFactory.PASSWORD.key, "Ucsp123#");
|
|
|
+ mapParams.put(DBCPDataSourceFactory.MAXACTIVE.key, Integer.valueOf(10));
|
|
|
+ mapParams.put(DBCPDataSourceFactory.MAXIDLE.key, Integer.valueOf(0));
|
|
|
+
|
|
|
+ DataSource source = DataSourceFinder.getDataSource(mapParams);
|
|
|
+
|
|
|
+ DataStore dataStore = DataStoreFinder.getDataStore(mapParams);
|
|
|
+
|
|
|
+
|
|
|
+ System.out.println("111111111111");
|
|
|
+
|
|
|
+ }
|
|
|
+}
|