GeoTools 是英国利兹大学(University of Leeds)的James Macgll 从1996 年开始研发的一个操作和显示地图的开源Java 代码库, [1] 提供了一系列符合规范的操作地理空间数据的方法,例如可以用它来实现一个地理信息系统(GIS)。GeoTools库在开发时执行OGC规范。目前GeoTools的最新版本是22-SNAPSHOT。 [2]
GeoTools是一个开源的Java GIS工具包,可利用它来开发符合标准的地理信息系统。GeoTools提供了OGC(Open Geospatial Consortium)规范的一个实现来作为他们的开发。
本文主要写的是利用GeoTools 连接shp文件,并对其进行增删改。
由于过于简单,就当笔记记录一下,时间宝贵的同学就不要看了。
由于geotools是一个GIS类库,所以直接用maven进行构建就可以。
详细可以参考官方教程:http://docs.geotools.org/
这个可以理解为平时Java连接数据的时候那个数据源。
1.gt-main模块geotools支持从一系列数据源访问特征信息(即向量信息)。其他数据存储插件可以从gt-jdbc中获得,用于数据库访问。
数据模块主要是将外部服务、磁盘文件等中的数据(通常以特性的形式)提升到应用程序中。这是您最终可以开始使用工具箱的地方。
而gt-main模块是实现附加DataStore的基础
所以要加入以下依赖
<dependency> <groupId>org.geotools</groupId> <artifactId>gt-main</artifactId> <version>${geotools.version}</version></dependency>
2. 获取数据源DataStore
官方文档不建议手工创建数据存储;相反,我们使用FactoryFinder,它将定位支持所请求格式的正确插件,也就是下面代码
public DataStore getDataStore(String shpPath) throws Exception { File file = new File(shpPath); Map params = new HashMap(); params.put("url", file.toURI().toURL());
for (Iterator i = DataStoreFinder.getAvailableDataStores(); i.hasNext(); ) { DataStoreFactorySpi factory = (DataStoreFactorySpi) i.next(); try { if (factory.canProcess(params)) { dataStore = factory.createNewDataStore(params); // 设置编码后可以正确读取 ((ShapefileDataStore) dataStore).setCharset(Charset.forName("GBK")); typeName = dataStore.getTypeNames()[0]; featureSource = dataStore.getFeatureSource(dataStore.getTypeNames()[0]); } } catch (Throwable warning) { warning.printStackTrace(); } } return null; }
这个东西可以跟平时的mybatis的那个SqlSession,将增删改进行了封装
它让您能够以Java对象的形式访问地理空间信息。
要查看特性资源,如果当前用户有权修改或锁定特性,则可以支持额外的接口特性恢复和特性锁定。
其中的SimpleFeatureSource就已经封装好了相应增删改的操作API
最终代码如下所列:
public void addFeature(List<String> dataList) throws IOException { System.out.println("============addFeature=====start====="); SimpleFeatureStore store = (SimpleFeatureStore) dataStore.getFeatureSource(typeName); SimpleFeatureType featureType = store.getSchema(); SimpleFeatureBuilder build = new SimpleFeatureBuilder(featureType); GeometryBuilder geom = new GeometryBuilder(); List<SimpleFeature> list = new ArrayList<>(); for (String row : dataList) { String[] rowArr = row.split(","); SimpleFeature feature = build.buildFeature(rowArr[0]); feature.setAttribute("ID", rowArr[0]); feature.setAttribute("NUM", rowArr[1]); feature.setAttribute("NAME", rowArr[2]); feature.setAttribute("MANAGER", rowArr[3]); feature.setAttribute("TYPE", rowArr[4]); feature.setAttribute("MESHID", rowArr[5]); feature.setAttribute("JDCODE", rowArr[6]); list.add(feature); }
SimpleFeatureCollection collection = new ListFeatureCollection(featureType, list); Transaction transaction = new DefaultTransaction(handle); store.setTransaction(transaction); try { store.addFeatures(collection); transaction.commit(); // actually writes out the features in one go System.out.println("============addFeature=====done====="); } catch (Exception eek) { eek.printStackTrace(); transaction.rollback(); } }
public void updateFeature(String id) throws IOException {
System.out.println("========updateFeature====start===="); long currentTimeMillis = System.currentTimeMillis(); Transaction transaction = new DefaultTransaction(handle); SimpleFeatureStore store = (SimpleFeatureStore) dataStore.getFeatureSource(typeName); store.setTransaction(transaction); FilterFactory2 ff = CommonFactoryFinder.getFilterFactory2(); Filter filter = ff.equals(ff.property("ID"), ff.literal(id)); SimpleFeatureType featureType = store.getSchema(); try { store.modifyFeatures("ROAD", "xbmchina.cn", filter); transaction.commit(); System.out.println("========updateFeature====end===="); } catch (Exception eek) { eek.printStackTrace(); transaction.rollback(); } long l = System.currentTimeMillis() - currentTimeMillis; System.out.println("耗时:"+l/1000.0);
}
public void removeFeature(Set<String> ids) throws IOException { System.out.println("======removeFeature== start ========"); long currentTimeMillis = System.currentTimeMillis(); Transaction transaction = new DefaultTransaction(handle); SimpleFeatureStore store = (SimpleFeatureStore) dataStore.getFeatureSource(typeName); store.setTransaction(transaction); FilterFactory2 ff = CommonFactoryFinder.getFilterFactory2(); List<Filter> match = new ArrayList<>(); for (String id : ids) { Filter aMatch = ff.equals(ff.property("ID"), ff.literal(id)); match.add(aMatch); } Filter filter = ff.or(match); try { store.removeFeatures(filter); transaction.commit(); System.out.println("======removeFeature== done ========"); } catch (Exception eek) { eek.printStackTrace(); transaction.rollback(); } long l = System.currentTimeMillis() - currentTimeMillis; System.out.println("耗时:"+l/1000.0); }
public void getFeaturesByDataID(String id) throws IOException, CQLException { System.out.println("======getByDataID====start====="); long currentTimeMillis = System.currentTimeMillis(); FilterFactory2 ff = CommonFactoryFinder.getFilterFactory2();// Filter filter = CQL.toFilter("ID = 'ef7872a5959448c8bfb24604adc07ada' "); Filter filter = ff.equals(ff.property("ID"), ff.literal(id)); FeatureCollection<SimpleFeatureType, SimpleFeature> features = featureSource.getFeatures(filter); System.out.println("features size:" + features.size()); FeatureIterator<SimpleFeature> featureIterator = features.features();// while (featureIterator.hasNext()) {// SimpleFeature feature = featureIterator.next();// Collection<Property> properties = feature.getProperties();// System.out.println(properties);// } System.out.println("======getByDataID====done====="); long l = System.currentTimeMillis() - currentTimeMillis; System.out.println("耗时:"+l/1000.0); }
http://docs.geotools.org/latest/userguide/library/data/index.html http://docs.geotools.org/latest/userguide/library/data/datastore.html http://docs.geotools.org/latest/userguide/library/data/featuresource.html
如果对 Java、大数据感兴趣请长按二维码关注一波,我会努力带给你们价值。觉得对你哪怕有一丁点帮助的请帮忙点个赞或者转发哦。关注公众号【爱编码】,小编会一直更新文章的哦。