GeoTools应用-DATA
转自:http://blog.csdn.net/cdl2008sky/article/details/7266785
一、Geotools The Open Source Java GIS Toolkit
http://geotools.org/ Geotools官方网站
http://docs.geotools.org/latest/javadocs/ Geotools API在线文档
http://docs.codehaus.org/display/GEOTDOC/Home Geotools用户指南
http://repo.opengeo.org Geotools的maven仓库地址
http://download.osgeo.org/webdav/geotools/ maven仓库地址
POM.xml配置
- <repositories>
- <repository>
- <id>osgeo</id>
- <name>Open Source Geospatial Foundation Repository</name>
- <url>http://download.osgeo.org/webdav/geotools/</url>
- </repository>
- <repository>
- <snapshots>
- <enabled>true</enabled>
- </snapshots>
- <id>opengeo</id>
- <name>OpenGeo Maven Repository</name>
- <url>http://repo.opengeo.org</url>
- </repository>
- </repositories>
eg:取到gt-main.jar的依赖关系
- <dependency>
- <groupId>org.geotools</groupId>
- <artifactId>gt-main</artifactId>
- <version>8.4</version>
- </dependency>
二、OpenGIS 软件架构

org.geotools.data
包负责地理数据的读写(如:ShavefileReader用于读取shpfile数据),org.geotools.geometry
包负责提供对JTs的调用接口,以将地理数据封装成JTS中定义的几何对象(Geometry),
org.geotools.feature包负责封装空间几何要素对象(Feature),对应于地图中一个实体,
包含:空间数据(Geometry)、属性数据(Aitribute)、参考坐标系(Refereneedsystem)、
最小外包矩形(EnveloPe)等属性,是Gls操作的核心数据模型。
Geotools 读取shp 数据格式的例子:
- /**
- * 读取shap格式的文件
- *
- * @param path
- */
- public void readSHP(String path) {
- ShapefileDataStore shpDataStore = null;
- try {
- shpDataStore = new ShapefileDataStore(new File(path).toURI()
- .toURL());
- shpDataStore.setStringCharset(Charset.forName("GBK"));
- // 文件名称
- String typeName = shpDataStore.getTypeNames()[0];
- FeatureSource<SimpleFeatureType, SimpleFeature> featureSource = null;
- featureSource = (FeatureSource<SimpleFeatureType, SimpleFeature>) shpDataStore
- .getFeatureSource(typeName);
- FeatureCollection<SimpleFeatureType, SimpleFeature> result = featureSource
- .getFeatures();
- SimpleFeatureType schema = result.getSchema(); // schema
- List<AttributeDescriptor> columns = schema
- .getAttributeDescriptors();
- FeatureIterator<SimpleFeature> itertor = result.features();
- /*
- * 或者使用 FeatureReader FeatureReader reader =
- * DataUtilities.reader(result); while(reader.hasNext()){
- * SimpleFeature feature = (SimpleFeature) reader.next(); }
- */
- while (itertor.hasNext()) {
- SimpleFeature feature = itertor.next();
- for (AttributeDescriptor attributeDes : columns) {
- String attributeName = attributeDes.getName().toString();// attribute
- if (attributeName.equals("the_geom"))
- continue;
- feature.getAttribute(attributeName); // attributeValue
- }
- Geometry g = (Geometry) feature.getDefaultGeometry();// Geometry
- }
- itertor.close();
- } catch (MalformedURLException e) {
- e.printStackTrace();
- } catch (IOException e) {
- e.printStackTrace();
- }
- }
- /**
- * 读取dbf格式的文件,只存储属性值,不存储空间值
- *
- * @param path
- */
- public void readDBF(String path) {
- DbaseFileReader reader = null;
- try {
- reader = new DbaseFileReader(new ShpFiles(path), false,
- Charset.forName("GBK"));
- DbaseFileHeader header = reader.getHeader();
- int numFields = header.getNumFields();
- for (int i = 0; i < numFields; i++) {
- header.getFieldName(i);
- header.getFieldType(i);// 'C','N'
- header.getFieldLength(i);
- }
- // 迭代读取记录
- while (reader.hasNext()) {
- try {
- Object[] entry = reader.readEntry();
- for (int i = 0; i < numFields; i++) {
- String title = header.getFieldName(i);
- Object value = entry[i];
- String name = title.toString(); // column
- String info = value.toString(); // value
- }
- } catch (Exception e) {
- e.printStackTrace();
- }
- }
- } catch (Exception ex) {
- ex.printStackTrace();
- } finally {
- if (reader != null) {
- // 关闭
- try {
- reader.close();
- } catch (Exception e) {
- }
- }
- }
- }
输出一个shp文件
- /**
- * 创建shp文件
- *
- * @param outPath
- */
- public void createShp(String outPath) {
- try {
- // 定义属性
- final SimpleFeatureType TYPE = DataUtilities.createType("Location",
- "location:Point," + "NAME:String," + "INFO:String,"
- + "OWNER:String");
- FeatureCollection<SimpleFeatureType, SimpleFeature> collection = FeatureCollections.newCollection();
- GeometryFactory geometryFactory = new GeometryFactory();
- SimpleFeatureBuilder featureBuilder = new SimpleFeatureBuilder(TYPE);
- double latitude = Double.parseDouble("116.123456789");
- double longitude = Double.parseDouble("39.120001");
- String NAME = "运通110路";
- String INFO = "白班车,学生票有效";
- String OWNER = "001";
- //创建坐标
- Point point = geometryFactory.createPoint(new Coordinate(longitude,latitude));
- //创建属性值
- Object[] obj = {point, NAME, INFO, OWNER };
- //构造一个Feature
- SimpleFeature feature = featureBuilder.buildFeature(null, obj);
- //添加到集合
- collection.add(feature);
- // shap文件的输出路径
- File newFile = new File(outPath);
- Map<String, Serializable> params = new HashMap<String, Serializable>();
- params.put("url", (Serializable) newFile.toURI().toURL());
- params.put("create spatial index", (Serializable) Boolean.TRUE);
- ShapefileDataStoreFactory dataStoreFactory = new ShapefileDataStoreFactory();
- ShapefileDataStore newDataStore = (ShapefileDataStore) dataStoreFactory
- .createNewDataStore(params);
- newDataStore.createSchema(TYPE);
- newDataStore.setStringCharset(Charset.forName("GBK"));
- newDataStore.forceSchemaCRS(DefaultGeographicCRS.WGS84);
- String typeName = newDataStore.getTypeNames()[0];
- ShapefileFeatureLocking featureSource = (ShapefileFeatureLocking) newDataStore
- .getFeatureSource(typeName);
- // 创建一个事务
- Transaction transaction = new DefaultTransaction("create");
- featureSource.setTransaction(transaction);
- try {
- featureSource.addFeatures(collection);
- // 提交事务
- transaction.commit();
- } catch (Exception problem) {
- problem.printStackTrace();
- transaction.rollback();
- } finally {
- transaction.close();
- }
- } catch (Exception e) {
- e.printStackTrace();
- }
- }
org.geotools.data.DataUtilities
a facade classes which can help simplify common data wrangling chores 简化繁琐的通用数据
(1)、定义属性
FeatureType TYPE = DataUtilities.createType("Location",
"location:Point," + "NAME:String," + "INFO:String,"+ "OWNER:String");
(2) DataUtilities.schema
You can use this method to quickly get a representation of a FeatureType 返回FeatureType的schema
//返回schema
DataUtilities.spec(featureType))
(3) DataUtilities.collection Feature数组转换为Feature集合
DataUtilities has helper methods to turn almost anything into a FeatureCollection
Feature[] array;
....
return DataUtilties.collection( array );
(4) DataUtilities.reader 格式化
convert a perfectly good collection to FeatureReader format.
FeatureCollection collection;
FeatureReader reader = DataUtilities.reader( collection );
附:shp 格式文件介绍
Shapefile file extensions
.shp—The main file that stores the feature geometry. Required.
.shx—The index file that stores the index of the feature geometry. Required.
.dbf—The dBASE table that stores the attribute information of features. Required.There is a one-to-one relationship between geometry and attributes, which is based on record number.
.prj—The file that stores the coordinate system information. Used by ArcGIS.
DBF文件中的数据类型FieldType
代码 数据类型 允许输入的数据
B 二进制型 各种字符。
C 字符型 各种字符。
D 日期型 用于区分年、月、日的数字和一个字符,内部存储按照YYYYMMDD格式。
G (Generalor OLE) 各种字符。
N 数值型(Numeric) - . 0 1 2 3 4 5 6 7 8 9
L 逻辑型(Logical)? Y y N n T t F f (? 表示没有初始化)。
M (Memo) 各种字符。
GeoTools应用-DATA的更多相关文章
- GeoTools介绍、环境安装、读取shp文件并显示
GeoTools是一个开放源代码(LGPL)Java代码库,它提供了符合标准的方法来处理地理空间数据,例如实现地理信息系统(GIS).GeoTools库实现了开放地理空间联盟(OGC)规范. Geot ...
- Spring-Boot ☞ ShapeFile文件读写工具类+接口调用
一.项目目录结构树 二.项目启动 三.往指定的shp文件里写内容 (1) json数据[Post] { "name":"test", "path&qu ...
- JAVA用geotools读写shape格式文件
转自:http://toplchx.iteye.com/blog/1335007 JAVA用geotools读写shape格式文件 (对应geotools版本:2.7.2) (后面添加对应geotoo ...
- geotools导入shp文件到Oracle数据库时表名带下划线的问题解决
问题: 最近在做利用geotools导入shp文件到Oracle表中,发现一个问题Oracle表名带下划线时导入失败,问题代码行: dsOracle.getFeatureWriterAppend(or ...
- maven构建geotools应用工程
前置条件 jdk1.7+eclipse+maven POM配置 <project xmlns="http://maven.apache.org/POM/4.0.0" xmln ...
- 简析服务端通过geotools导入SHP至PG的方法
文章版权由作者李晓晖和博客园共有,若转载请于明显处标明出处:http://www.cnblogs.com/naaoveGIS/ 1.背景 项目中需要在浏览器端直接上传SHP后服务端进行数据的自动入PG ...
- geotools中泰森多边形的生成
概述 本文讲述如何在geotools中生成泰森多边形,并shp输出. 泰森多边形 1.定义 泰森多边形又叫冯洛诺伊图(Voronoi diagram),得名于Georgy Voronoi,是由一组由连 ...
- 说说geotools中坐标转换那点事
概述: 本文说说geotools中坐标转换的那点事情,以WGS84和web墨卡托相互转换为例. 效果: 转换前 转换后 单个Geometry转换 实现代码: package com.lzugis.ge ...
- geotools修改shapefile 属性名乱码问题
在GeoServer中文社区的讨论地址为:http://opengeo.cn/bbs/read.php?tid=1701&page=e&#a 使用geotools修改shapefile ...
随机推荐
- Java中正确使用hashCode() 和equals() 、==
在java编程中,经常会遇到两个对象中某个属性的比较,常常会用到的方法有: == .equals().但是两者使用起来有什么区别呢? 一.== java中的==是比较两个对象在JVM中的地址.比较好理 ...
- Gradle实战教程之依赖管理
这是从我个人网站中复制过来的,原文地址:http://coolshell.info/blog/2015/05/gradle-dependency-management.html,转载请注明出处. 简要 ...
- System V 消息队列
3.1 概述 消息队列结构: struct msqid_ds { struct ipc_perm msg_perm; //权限结构 struct msg *msg_first; //队列中第一个消息 ...
- (转)IOS内存管理 retain release
obj-c本质就是"改进过的c语言",大家都知道c语言是没有垃圾回收(GC)机制的(注:虽然obj-c2.0后来增加了GC功能,但是在iphone上不能用,因此对于iOS平台的程序 ...
- 多语言文本资源的访问(Windows:ini)
目标 本文要讨论对于开发多语言界面程序所需要解决的一个问题,即文本资源组织及访问的方法. 本文主要以Windows平台下讨论具现并提供处理代码. Windows方案 Windows下界面开发,除Dir ...
- 负载均衡-多台机子session不起效:把php.ini中file改为memcache存储
一 开启memcache服务 二 修改php.ini中session配置 php/lib/php.ini session.save_handler = memcache session.save_pa ...
- XStream简单使用01——xml和Ojbect互转
package org.zhb.test; /** * author : zhb * data : 2014-2-14 * use packages: * xmlpull-1.1.3.1.jar * ...
- php中mysqli 处理查询结果集的几个方法
最近对php查询mysql处理结果集的几个方法不太明白的地方查阅了资料,在此整理记下 Php使用mysqli_result类处理结果集有以下几种方法 fetch_all() 抓取所有的结果行并且以关联 ...
- python【第二十篇】Django表的多对多、Ajax
1 创建多对多表的方式有两种 1.1 方式一:自定义关系表 class Host(models.Model): nid = models.AutoField(primary_key=True) hos ...
- 成为IT经理必备的十大软技能
对于一个IT从业者,让你谋得工作的也许是技术能力,但有助于提升职业生涯的却是软技能.步步高升的人都是那些发表文章.在会议上积极发言以及关注客户的员工(程序员).与此同时,通常情况下,企业CIO或多或少 ...