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 ...
随机推荐
- 脱离Xcode,程序在模拟器中无法运行
今天在调试项目的时候 突然发现,如果项目不通过Xcode启动而是直接通过模拟器进行启动,程序闪一下马上退出,并且不是闪退,而是跑到后台去了,并且后台的程序同样无法启动.找了好多解决办法,最后的解决方案 ...
- 原生js的数组除重复
js对数组的操作在平常的项目中也会遇到,除去一些增加,或者减少的操作外,还有一个比较重要的操作就是数组的除重,通过数组的除重,我们可以将一个数组中存在的多个重复的数组进行清理,只留下不重复的.另外下面 ...
- Myeclipse下不用dom4j等解析xml文档
- RSA安全性问题
加密:C=Me(mod n) 解密:M=Cd(mod n) 安全性基础: 穷举法攻击: 1.攻击者设计一个M,C=Me(mod n) 2.d的个数至多有n-1个,尝试使用每个d破解,如果M’=Cd‘( ...
- yum 安装 依赖报错
今天使用yum安装的时候 报错: Error: Multilib version problems found. This often means that the root cause 应该是yum ...
- 查找PHP的配置文件
查找PHP的配置文件 先写了一个 <?php phpinfo();?>然后在浏览器中浏览一下(之前我百度说在Configuration File 这个位置看) 结果竟然显示 Loaded ...
- softmax
void LogisticRegression_softmax(LogisticRegression *this, double *x) { int i; double max = 0.0; doub ...
- jquery判断复选框是否选中
jquery判断复选框是否被选中 $(function(){ $(document).on("click", ".checkbox",function(){ v ...
- FastCgi与PHP-fpm关系[转] 读完本文瞬间明朗了很多
刚开始对这个问题我也挺纠结的,看了<HTTP权威指南>后,感觉清晰了不少. 首先,CGI是干嘛的?CGI是为了保证web server传递过来的数据是标准格式的,方便CGI程序的编写者. ...
- 学习TextKit框架(上)
TextKit简介 在iOS7之前我们要实现图文混排要使用CoreText,iOS6时有了Attribute string 可以解决一些简单的富文本需求.直到iOS7 苹果推出了TextKit,Tex ...