- 浏览: 7250903 次
- 性别:
- 来自: 上海
文章分类
- 全部博客 (1546)
- 企业中间件 (236)
- 企业应用面临的问题 (236)
- 小布Oracle学习笔记汇总 (36)
- Spring 开发应用 (54)
- IBatis开发应用 (16)
- Oracle基础学习 (23)
- struts2.0 (41)
- JVM&ClassLoader&GC (16)
- JQuery的开发应用 (17)
- WebService的开发应用 (21)
- Java&Socket (44)
- 开源组件的应用 (254)
- 常用Javascript的开发应用 (28)
- J2EE开发技术指南 (163)
- EJB3开发应用 (11)
- GIS&Mobile&MAP (36)
- SWT-GEF-RCP (52)
- 算法&数据结构 (6)
- Apache开源组件研究 (62)
- Hibernate 学习应用 (57)
- java并发编程 (59)
- MySQL&Mongodb&MS/SQL (15)
- Oracle数据库实验室 (55)
- 搜索引擎的开发应用 (34)
- 软件工程师笔试经典 (14)
- 其他杂项 (10)
- AndroidPn& MQTT&C2DM&推技术 (29)
- ActiveMQ学习和研究 (38)
- Google技术应用开发和API分析 (11)
- flex的学习总结 (59)
- 项目中一点总结 (20)
- java疑惑 java面向对象编程 (28)
- Android 开发学习 (133)
- linux和UNIX的总结 (37)
- Titanium学习总结 (20)
- JQueryMobile学习总结 (34)
- Phonegap学习总结 (32)
- HTML5学习总结 (41)
- JeeCMS研究和理解分析 (9)
最新评论
-
lgh1992314:
[u][i][b][flash=200,200][url][i ...
看看mybatis 源代码 -
尼古拉斯.fwp:
图片根本就不出来好吧。。。。。。
Android文件图片上传的详细讲解(一)HTTP multipart/form-data 上传报文格式实现手机端上传 -
ln94223:
第一个应该用排它网关吧 怎么是并行网关, 并行网关是所有exe ...
工作流Activiti的学习总结(八)Activiti自动执行的应用 -
ZY199266:
获取不到任何消息信息,请问这是什么原因呢?
ActiveMQ 通过JMX监控Connection,Queue,Topic的信息 -
xiaoyao霄:
DestinationSourceMonitor 报错 应该导 ...
ActiveMQ 通过JMX监控Connection,Queue,Topic的信息
GIS的学习(二十六)geotools 使用 部分代码总结
前段时间的一个项目 本来用ae完成了种种的 查询,空间分析等等功能的代码,但是不幸的是 这是一个web项目,无奈 ae各种错误,显然ae放在server端是不好使的 无奈 一咬牙一跺脚 全部换 换成geotools 看文档 看api 从零 开始算是把 原来AE实现的东西 统统改了过来 用起来 反而觉得既稳定 效率还不错哈!
以下是部分功能总结:
1、连接数据库 这里使用的postgis 链接代码如下:
private static void conn(String dbtype, String host, String port, String database, String userName, String password) { Map<String, Object> params = new HashMap<String, Object>(); // params.put(PostgisNGDataStoreFactory.DBTYPE.key, "postgis"); // 两种代码方式 // params.put(PostgisNGDataStoreFactory.HOST.key, "localhost"); // params.put(PostgisNGDataStoreFactory.PORT.key, new Integer(5432)); // params.put(PostgisNGDataStoreFactory.DATABASE.key, "postgis"); // params.put(PostgisNGDataStoreFactory.SCHEMA.key, "public"); // params.put(PostgisNGDataStoreFactory.USER.key, "postgres"); // params.put(PostgisNGDataStoreFactory.PASSWD.key, "root"); params.put(PostgisNGDataStoreFactory.DBTYPE.key, dbtype); params.put(PostgisNGDataStoreFactory.HOST.key, host); params.put(PostgisNGDataStoreFactory.PORT.key, new Integer(port)); params.put(PostgisNGDataStoreFactory.DATABASE.key, database); params.put(PostgisNGDataStoreFactory.SCHEMA.key, "public"); params.put(PostgisNGDataStoreFactory.USER.key, userName); params.put(PostgisNGDataStoreFactory.PASSWD.key, password); try { pgDatastore = DataStoreFinder.getDataStore(params); if (pgDatastore != null) { System.out.println("系统连接到位于:" + host + "的空间数据库" + database + "成功!"); } else { System.out.println("系统连接到位于:" + host + "的空间数据库" + database + "失败!请检查相关参数"); } } catch (IOException e) { e.printStackTrace(); System.out.println("系统连接到位于:" + host + "的空间数据库" + database + "失败!请检查相关参数"); } } 调用方法为:conn("postgis", "localhost", 5432, "postgis", "postgres", "root");
2、图层的操作
2.1 查询 public static ArrayList<SimpleFeature> queryMethod(String filterStr, String layerName) { //pgDatastore为上文连接数据库获取相当于AE中的workspace //SimpleFeatureSource相当于AE中的featureClass SimpleFeatureSource featureSource =pgDatastore.getFeatureSource(layerName); ArrayList<SimpleFeature> featureList = new ArrayList<SimpleFeature>(); if(featureSource==null) return featureList; try { Filter filter; filter = CQL.toFilter(filterStr); // filterStr形式 如 name='武汉大学' or code like 'tt123%' SimpleFeatureCollection result = featureSource.getFeatures(filter); FeatureIterator<SimpleFeature> itertor = result.features(); while (itertor.hasNext()) { SimpleFeature feature = itertor.next(); featureList.add(feature); } itertor.close(); return featureList; } catch (CQLException e) { // TODO Auto-generated catch block e.printStackTrace(); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } return null; }
2.2 要素操作 对上面4.1中的 SimpleFeature操作 //获取feature的geometry Geometry geo=(Geometry) feature.getDefaultGeometry(); //获取geometry中的坐标 这里用string的方式保存 int geoUnm = geo.getNumGeometries(); // 一个geometry可能含有n个geometry for (int i = 0; i < geoUnm; i++) { Geometry singleGeo = geo.getGeometryN(i); //获取其中每一个geometry int pointCount = singleGeo.getNumPoints(); Coordinate[] coords = singleGeo.getCoordinates(); for (int j = 0; j < pointCount; j++) { if (j == pointCount - 1) sBuilder.append(coords[j].x + "," + coords[j].y); else { sBuilder.append(coords[j].x + "," + coords[j].y + ";"); } } if (i != geoUnm - 1) { sBuilder.append("|"); } } //获取feature中的属性 feature.getAttribute(arg0);
2.3 拓扑查询 public static Filter getGeoFilter(FilterFactory2 ff, //构建拓扑查询的filter String geometryAttributeName, Geometry refGeo, SpatialReltionType.TopoRelTypeEnum relType) { //这个SpatialReltionType是我自己定义的。。。 switch (relType) { case intersect: return ff.intersects(ff.property(geometryAttributeName), ff .literal(refGeo)); case contains: return ff.contains(ff.property(geometryAttributeName), ff .literal(refGeo)); case within: return ff.within(ff.property(geometryAttributeName), ff .literal(refGeo)); case cross: return ff.crosses(ff.property(geometryAttributeName), ff .literal(refGeo)); case overlaps: return ff.overlaps(ff.property(geometryAttributeName), ff .literal(refGeo)); case touches: return ff.touches(ff.property(geometryAttributeName), ff .literal(refGeo)); case equals: return ff.equals(ff.property(geometryAttributeName), ff .literal(refGeo)); case disjoint: return ff.disjoint(ff.property(geometryAttributeName), ff .literal(refGeo)); default: return null; } }
// 普通的拓扑查询 public static ArrayList<Geometry> topoQueryMethod(Geometry refGeo, String layerName, SpatialReltionType.TopoRelTypeEnum relType) { ArrayList<SimpleFeature> featurelist=new ArrayList<SimpleFeature>(); FilterFactory2 ff = CommonFactoryFinder.getFilterFactory2(null); SimpleFeatureSource featureSource=pgDatastore.getFeatureSource(layerName); SimpleFeatureType schema = featureSource.getSchema(); String geometryAttributeName = schema.getGeometryDescriptor().getLocalName(); Filter filter1= getGeoFilter(ff,geometryAttributeName, refGeo, relType); //上面的方法 SimpleFeatureCollection result=null; try { result = featureSource.getFeatures(filter1); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } if(result==null) return null; FeatureIterator<SimpleFeature> itertor = result.features(); while (itertor.hasNext()) { SimpleFeature feature = itertor.next(); featurelist.add(feature); } //这个方法是将feature转为geometry 自己定义的 return SpatialUtil.ConverToGeoList(featurelist); }
//联合属性的拓扑查询 public static ArrayList<Geometry> topoQueryMethod(Geometry refGeo, String queryName, String layerName, SpatialReltionType.TopoRelTypeEnum relType) { FilterFactory2 ff = CommonFactoryFinder.getFilterFactory2(null); ArrayList<SimpleFeature> featurelist=new ArrayList<SimpleFeature>(); SimpleFeatureSource featureSource=pgDatastore.getFeatureSource(layerName); SimpleFeatureType schema = featureSource.getSchema(); String geometryAttributeName = schema.getGeometryDescriptor().getLocalName(); Filter filter1= SpatialUtil.getGeoFilter(ff,geometryAttributeName, refGeo, relType); Filter filter2=null; try { filter2=CQL.toFilter("StandName = '"+queryName+"'"); } catch (CQLException e1) { // TODO Auto-generated catch block e1.printStackTrace(); } List<Filter> match = new ArrayList<Filter>(); match.add(filter1); match.add(filter2); Filter filter = ff.and(match); SimpleFeatureCollection result=null; try { result = featureSource.getFeatures(filter); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } if(result==null) return null; FeatureIterator<SimpleFeature> itertor = result.features(); while (itertor.hasNext()) { SimpleFeature feature = itertor.next(); featurelist.add(feature); } return SpatialUtil.ConverToGeoList(featurelist); }
3,编辑图层
3.1 添加要素 //添加一个feature到图层中 在添加前要确定构造featureType public static SimpleFeatureType createFeatureType(String typeName,Class type) { SimpleFeatureTypeBuilder builder = new SimpleFeatureTypeBuilder(); builder.setName(typeName); builder.setCRS(DefaultGeographicCRS.WGS84); // <- Coordinate reference // system builder.add("the_geom", type); //这个为地理属性字段 postgis中为 the——geom builder.add("StandName", String.class); // 这是其他属性字段 自己定义的.... // build the type final SimpleFeatureType TYPE = builder.buildFeatureType(); return TYPE; } //添加到图层的图层名,添加的要素空间属性和要素的某属性名 public static boolean addFeature(String layerName,Geometry geo,String featureName){ String type=geo.getGeometryType(); Class TypeClass=null; if(type.toLowerCase().equals("point")){ TypeClass=Point.class; }else if(type.toLowerCase().equals("polygon")){ TypeClass=Polygon.class; }else if(type.toLowerCase().equals("polyline")){ TypeClass=Polyline.class; }else if(type.toLowerCase().equals("multipolygon")){ TypeClass=MultiPolygon.class; } SimpleFeatureType featureType=createFeatureType(layerName,TypeClass); SimpleFeatureCollection collection = FeatureCollections.newCollection(); SimpleFeatureBuilder featureBuilder = new SimpleFeatureBuilder(featureType); /* Longitude (= x coord) first ! */ GeometryFactory geometryFactory = JTSFactoryFinder.getGeometryFactory(null); featureBuilder.add(geo); featureBuilder.add(featureName); SimpleFeature feature = featureBuilder.buildFeature(null); collection.add(feature); FeatureSource featureSource=pgDatastore.getFeatureSource(layerName); if (featureSource instanceof SimpleFeatureStore) { SimpleFeatureStore featureStore = (SimpleFeatureStore) featureSource; Transaction transaction = new DefaultTransaction("create"); featureStore.setTransaction(transaction); try { featureStore.addFeatures(collection); transaction.commit(); return true; } catch (Exception problem) { problem.printStackTrace(); try { transaction.rollback(); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } } finally { try { transaction.close(); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } } } else { System.out.println(layerName + " does not support read/write access"); } return false; }
3.2 修改要素 // 修改feacode为XX的要素的名字为featureName 地理方位为geo (feacode StandName为你的属性字段自定义) public static boolean modifyFeature(String layerName,Geometry geo,String featureName,String FeaCode){ FeatureSource featureSource=pgDatastore.getFeatureSource(layerName); if (featureSource instanceof SimpleFeatureStore) { SimpleFeatureStore featureStore = (SimpleFeatureStore) featureSource; Transaction transaction = new DefaultTransaction("create"); featureStore.setTransaction(transaction); try { String filterStr="FeaCode= '"+FeaCode+"'"; String[] names=new String[2]; names[0]="StandName"; names[1]="the_geom"; Object[] values=new Object[2]; values[0]=featureName; values[1]=geo; featureStore.modifyFeatures(names, values, CQL.toFilter(filterStr)); transaction.commit(); return true; } catch (Exception problem) { problem.printStackTrace(); try { transaction.rollback(); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } } finally { try { transaction.close(); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } } } else { System.out.println(layerName + " does not support read/write access"); } return false; }
4 、Geometry 与 JTS
geotools 构建 geometry方法:这里转载一个别人写的比较好的
4.1构建点 public Point createPoint(){ Coordinate coord = new Coordinate(109.013388, 32.715519); Point point = geometryFactory.createPoint( coord ); return point; } public Point createPointByWKT() throws ParseException{ WKTReader reader = new WKTReader( geometryFactory ); Point point = (Point) reader.read("POINT (109.013388 32.715519)"); return point; } public MultiPoint createMulPointByWKT()throws ParseException{ WKTReader reader = new WKTReader( geometryFactory ); MultiPoint mpoint = (MultiPoint) reader.read("MULTIPOINT(109.013388 32.715519,119.32488 31.435678)"); return mpoint; }
4.2 构建线 public LineString createLine(){ Coordinate[] coords = new Coordinate[] {new Coordinate(2, 2), new Coordinate(2, 2)}; LineString line = geometryFactory.createLineString(coords); return line; } public LineString createLineByWKT() throws ParseException{ WKTReader reader = new WKTReader( geometryFactory ); LineString line = (LineString) reader.read("LINESTRING(0 0, 2 0)"); return line; } public MultiLineString createMLine(){ Coordinate[] coords1 = new Coordinate[] {new Coordinate(2, 2), new Coordinate(2, 2)}; LineString line1 = geometryFactory.createLineString(coords1); Coordinate[] coords2 = new Coordinate[] {new Coordinate(2, 2), new Coordinate(2, 2)}; LineString line2 = geometryFactory.createLineString(coords2); LineString[] lineStrings = new LineString[2]; lineStrings[0]= line1; lineStrings[1] = line2; MultiLineString ms = geometryFactory.createMultiLineString(lineStrings); return ms; } public MultiLineString createMLineByWKT()throws ParseException{ WKTReader reader = new WKTReader( geometryFactory ); MultiLineString line = (MultiLineString) reader.read("MULTILINESTRING((0 0, 2 0),(1 1,2 2))"); return line; }
4.3 构建多边形 public Polygon createPolygonByWKT() throws ParseException{ WKTReader reader = new WKTReader( geometryFactory ); Polygon polygon = (Polygon) reader.read("POLYGON((20 10, 30 0, 40 10, 30 20, 20 10))"); return polygon; } public MultiPolygon createMulPolygonByWKT() throws ParseException{ WKTReader reader = new WKTReader( geometryFactory ); MultiPolygon mpolygon = (MultiPolygon) reader.read("MULTIPOLYGON(((40 10, 30 0, 40 10, 30 20, 40 10),(30 10, 30 0, 40 10, 30 20, 30 10)))"); return mpolygon; }
4.4 构建geo集合 public GeometryCollection createGeoCollect() throws ParseException{ LineString line = createLine(); Polygon poly = createPolygonByWKT(); Geometry g1 = geometryFactory.createGeometry(line); Geometry g2 = geometryFactory.createGeometry(poly); Geometry[] garray = new Geometry[]{g1,g2}; GeometryCollection gc = geometryFactory.createGeometryCollection(garray); return gc; }
4.5 构建圆 public Polygon createCircle(double x, double y, final double RADIUS){ final int SIDES = 32;//圆上面的点个数 Coordinate coords[] = new Coordinate[SIDES+1]; for( int i = 0; i < SIDES; i++){ double angle = ((double) i / (double) SIDES) * Math.PI * 2.0; double dx = Math.cos( angle ) * RADIUS; double dy = Math.sin( angle ) * RADIUS; coords[i] = new Coordinate( (double) x + dx, (double) y + dy ); } coords[SIDES] = coords[0]; LinearRing ring = geometryFactory.createLinearRing( coords ); Polygon polygon = geometryFactory.createPolygon( ring, null ); return polygon; }
postgis 删除表 SELECT DropGeometryTable ('my_schema','my_spatial_table');
如: SELECT DropGeometryTable ('public','river');
发表评论
-
【转】Django resources
2014-01-23 14:35 10716Django resources This page li ... -
使用国内镜像源来加速python pypi包的安装
2014-01-16 11:16 197561pipy国内镜像目前有: http://pypi.d ... -
[转 ]vagrant使用简介
2014-01-10 13:53 256141> 简介: vagrant提供了易于配置,重复性 ... -
[转]在Java中调用Python
2014-01-07 13:08 9138在执行之前都需要把jython对应的包加载进去,这个是必须的 ... -
[转]Eclipse配置PyDev插件
2014-01-02 14:25 2778安装python解释器 安装PyDev: 首 ... -
RestFuse的研究(五) Http请求的封装
2014-06-14 15:50 3513在RestFuse中封装了Http请 ... -
RestFuse的研究(四) Junit的Statement的分析
2013-12-06 11:46 1602在RestFuse提供了多种单 ... -
RestFuse的研究(三) Junit的Rule的使用和分析
2013-12-06 11:01 2190在junit中定义一些可以公用的规则(R ... -
RestFuse的研究(二) Junit的Runner的分类和模式
2013-12-06 10:40 1551在Junit4中的调用JunitCore可以采 ... -
RestFuse的研究(一) HttpJunitRunner的实现
2013-12-06 10:11 1691在RestFuse是一种针对Rest We ... -
[转]An open-source JUnit extension to test HTTP/REST APIs
2013-12-06 09:57 1052http://developer.eclipsesource ... -
TestNG简单的学习(十三)TestNG中Junit的实现
2013-12-04 09:00 3287TestNG和junit的整合 ... -
TestNG简单的学习(十二)TestNG运行
2013-12-03 09:08 51444文档来自官方地址: ... -
TestNG简单的学习(十一)TestNG学习总结
2013-12-03 09:08 13994最近一直在学习关于TestNG方面的知识,根 ... -
TestNG简单的学习(十)TestNG @Listeners 的使用
2013-12-03 09:07 8624TestNG官方网站: http://testng.or ... -
TestNG简单的学习(九)TestNG Method Interceptors 的使用
2013-12-03 09:07 2654TestNG官方网站: http://testng ... -
TestNG简单的学习(八)TestNG Annotation Transformers 的使用
2013-12-03 09:07 2757TestNG官方网站: http://testng.or ... -
TestNG简单的学习(七)TestNG编程方式运行
2013-12-02 09:22 2396TestNG官方网站: http://testng.or ... -
TestNG简单的学习(六)测试工厂注释的使用
2013-12-02 09:22 2713TestNG官方网站: http://testng.or ... -
TestNG简单的学习(五)参数化测试数据的定制
2013-12-02 09:22 2641TestNG官方网站: http://testng.or ...
相关推荐
org.geotools org.geotools.arcsde org.geotools.arcsde.data org.geotools.arcsde.data.versioning org.geotools.arcsde.data.view org.geotools.arcsde.filter org.geotools.arcsde.gce org.geotools....
为了在android下开发GIS.本文将GEOtools Googlemap ucmap 三种地图引擎各种优缺点作了比较,并实际代码测试过三种引擎,方便大家引擎选择.
org.geotools org.geotools.arcsde org.geotools.arcsde.data org.geotools.arcsde.data.view org.geotools.arcsde.filter org.geotools.arcsde.jndi org.geotools.arcsde.logging org.geotools.arcsde....
Quickstart 代码如下: import java.io.File; import org.geotools.data.FileDataStore; import org.geotools.data.FileDataStoreFinder; import org.geotools.data.simple.SimpleFeatureSource; import org.geo...
geotools学习(一)IntelliJ IDEA搭建快速入门示例.pdf
geotools-26.0.zip 代码和环境还有生成的jar 包
geotools汉语版资料
GeoTools是一个开源(LGPL)Java代码库,它为操作地理空间数据提供符合标准的方法,例如实现地理信息系统(GIS)。GeoTools库在开发时实现了开放地理空间联盟(OGC)规范。本文档为GeoTools 21.2 api文档
geotools整理的学习资料,详细的详述了geotools各个api,包括geoapi和jts。初学者的入门指南
geotools中文资料,我学习时候,整理的资料, 另外我的新浪博客,有关于gis 的flex -js-android-ios资料 欢迎下载,大家一起学习,我建了个gis群 291301205 新浪 http://blog.sina.com.cn/skywalkershaka 因为我下...
springboot项目-基于geotools的倾斜摄影大地坐标转自定义投影坐标源码.zipspringboot项目-基于geotools的倾斜摄影大地坐标转自定义投影坐标源码.zipspringboot项目-基于geotools的倾斜摄影大地坐标转自定义投影坐标...
geotools依赖包
英文版本,不是中文版本的 GeoTools开发的工具
geotools中文资料,我学习时候,整理的资料,
geotools对wms的实现支持,给出了geotools如何开发一个简单的wms实现,对理解geoserver源码有很大的帮助作用。
geotools所需jar包合集,内含4个geotools的jar包,各位可以根据需要自行导入使用,预祝bug少少
geotools 判断几何要素的交点 当时想到用的GDAL 但是 交点函数返回的对象总是null , 改用 GeoTools 这个库,需要用到jar 到官网上下载,主要是jts-core-1.16.0.jar
等值线等值面功能实现.使用技术包括Java+Geotools+WContour+Openlayers.包括整套开发示例代码及其相关jar包,下载即可使用。