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经验总结二:ORA-08103: 对象不再存在
问题发生的环境: 在springMvc+mybatis框架中,调用oracle的存储过程时,碰到的一个这样的异常: org.springframework.jdbc.UncategorizedSQLE ...
- swift入门-day01
Swift 简介 简介 Swift 语言由苹果公司在 2014 年推出,用来撰写 OS X 和 iOS 应用程序 2014 年,在 Apple WWDC 发布 历史 2010 年 7 月,苹果开发者工 ...
- SVN的那些事
1,终端报错:is too old (format 29) to work with client version '1.9.4 (r1740329)' (expects format 31). Yo ...
- javaee学习-servlet初始化参数
1.需要定义ServletConfig对象来接收servlet配置的初始化参数. 2.当servlet配置了初始化参数后,web容器在创建servlet实例对象时, 会自动将这些初始化参数封装到Ser ...
- bzoj3571: [Hnoi2014]画框 最小乘积匹配+最小乘积XX总结,
思路大概同bzoj2395(传送门:http://www.cnblogs.com/DUXT/p/5739864.html),还是将每一种匹配方案的Σai看成x,Σbi看成y,然后将每种方案转化为平面上 ...
- IOS 学习笔记 2015-03-18
Objective--C 一 关键字 1 KVC 动态设值,动态取值,类似雨java中的反射,而且私有的照样可以设置与获取 2 二 函数 1 retain 给对象引用计数器 + 1 2 release ...
- 在.NET 应用程序设计中如何选择Class, Abstract Class and Interface
关键字: Type– 类型 Class - 类 Abstract - 抽象的 Interface - 接口 Member - 成员 Method - 方法 Property - 属性 预备知识:在阅读 ...
- ECMA5.1中Object.seal()和Object.freeze()的区别
1 Object.seal(O)的调用 When the seal function is called, the following steps are taken: If Type(O) i ...
- 基于NodeJs的网页爬虫的构建(一)
好久没写博客了,这段时间已经忙成狗,半年时间就这么没了,必须得做一下总结否则白忙.接下去可能会有一系列的总结,都是关于定向爬虫(干了好几个月后才知道这个名词)的构建方法,实现平台是Node.JS. 背 ...
- cp 命令参数
cp命令 该命令的功能是将给出的文件或目录拷贝到另一文件或目录中,同MSDOS下的copy命令一样,功能十分强大. 语法: cp [选项] 源文件或目录 目标文件或目录 ...