java geoTools几何关系查询

该博客介绍了如何使用GeoTools进行数据查询,重点在于利用CQL(Common Query Language)进行空间过滤。示例代码展示了通过HashMap设置数据库连接参数,建立DataStore并获取FeatureSource,然后运用CQL过滤器筛选出与特定几何对象相交的特征。最后,遍历查询结果并打印出属性值。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

 @SneakyThrows
    public static void main(String[] args) {
        try {
            HashMap<String, Object> params = new HashMap<>(8);
            params.put("dbtype", "sqlserver");
            params.put("host", "ip");
            params.put("port", 1433);
            params.put("database", "geo");
            params.put("user", "sa");
            params.put("passwd", "pwd");
            DataStore dataStore = DataStoreFinder.getDataStore(params);
            SimpleFeatureSource source = dataStore.getFeatureSource("BGPOINT");
            SimpleFeatureType schema = source.getSchema();
            dataStore.getFeatureWriter(schema.getTypeName(),new DefaultTransaction());
            //name 一般为the_geom,表示记录shp的空间信息
            String name = schema.getGeometryDescriptor().getLocalName();
            Filter filter = CQL.toFilter("INTERSECTS("+name+",LINESTRING (13362219.971500002 3362953.5067000017, 13362227.4923 3362951.6404))");
            //关键是下面的代码
            Query query = new Query(schema.getTypeName(), filter, name);
            SimpleFeatureCollection features = source.getFeatures(query);
            SimpleFeatureIterator simpleFeatureIterator = features.features();
            while (simpleFeatureIterator.hasNext()){
                SimpleFeature simpleFeature = simpleFeatureIterator.next();
                Collection<Property> properties= simpleFeature.getProperties();
                Iterator<Property> iterator= properties.iterator();
                System.out.println(simpleFeature.getID());
                while (iterator.hasNext()){
                    Property next = iterator.next();
                    System.out.println(next.getValue());
                }

            }

        } catch (IOException e) {
            e.printStackTrace();

        }


    }

GeoTools入门(六)-- 查询那点事_surpassLiang的博客-CSDN博客 CQL使用

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值