转自: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配置

  1. <repositories>
  2. <repository>
  3. <id>osgeo</id>
  4. <name>Open Source Geospatial Foundation Repository</name>
  5. <url>http://download.osgeo.org/webdav/geotools/</url>
  6. </repository>
  7. <repository>
  8. <snapshots>
  9. <enabled>true</enabled>
  10. </snapshots>
  11. <id>opengeo</id>
  12. <name>OpenGeo Maven Repository</name>
  13. <url>http://repo.opengeo.org</url>
  14. </repository>
  15. </repositories>

eg:取到gt-main.jar的依赖关系

  1. <dependency>
  2. <groupId>org.geotools</groupId>
  3. <artifactId>gt-main</artifactId>
  4. <version>8.4</version>
  5. </dependency>

二、OpenGIS 软件架构

org.geotools.data
包负责地理数据的读写(如:ShavefileReader用于读取shpfile数据),org.geotools.geometry
包负责提供对JTs的调用接口,以将地理数据封装成JTS中定义的几何对象(Geometry),
org.geotools.feature包负责封装空间几何要素对象(Feature),对应于地图中一个实体,
包含:空间数据(Geometry)、属性数据(Aitribute)、参考坐标系(Refereneedsystem)、
最小外包矩形(EnveloPe)等属性,是Gls操作的核心数据模型。

Geotools 读取shp 数据格式的例子:

  1. /**
  2. * 读取shap格式的文件
  3. *
  4. * @param path
  5. */
  6. public void readSHP(String path) {
  7. ShapefileDataStore shpDataStore = null;
  8. try {
  9. shpDataStore = new ShapefileDataStore(new File(path).toURI()
  10. .toURL());
  11. shpDataStore.setStringCharset(Charset.forName("GBK"));
  12. // 文件名称
  13. String typeName = shpDataStore.getTypeNames()[0];
  14. FeatureSource<SimpleFeatureType, SimpleFeature> featureSource = null;
  15. featureSource = (FeatureSource<SimpleFeatureType, SimpleFeature>) shpDataStore
  16. .getFeatureSource(typeName);
  17. FeatureCollection<SimpleFeatureType, SimpleFeature> result = featureSource
  18. .getFeatures();
  19. SimpleFeatureType schema = result.getSchema(); // schema
  20. List<AttributeDescriptor> columns = schema
  21. .getAttributeDescriptors();
  22. FeatureIterator<SimpleFeature> itertor = result.features();
  23. /*
  24. * 或者使用 FeatureReader FeatureReader reader =
  25. * DataUtilities.reader(result); while(reader.hasNext()){
  26. * SimpleFeature feature = (SimpleFeature) reader.next(); }
  27. */
  28. while (itertor.hasNext()) {
  29. SimpleFeature feature = itertor.next();
  30. for (AttributeDescriptor attributeDes : columns) {
  31. String attributeName = attributeDes.getName().toString();// attribute
  32. if (attributeName.equals("the_geom"))
  33. continue;
  34. feature.getAttribute(attributeName); // attributeValue
  35. }
  36. Geometry g = (Geometry) feature.getDefaultGeometry();// Geometry
  37. }
  38. itertor.close();
  39. } catch (MalformedURLException e) {
  40. e.printStackTrace();
  41. } catch (IOException e) {
  42. e.printStackTrace();
  43. }
  44. }
  1. /**
  2. * 读取dbf格式的文件,只存储属性值,不存储空间值
  3. *
  4. * @param path
  5. */
  6. public void readDBF(String path) {
  7. DbaseFileReader reader = null;
  8. try {
  9. reader = new DbaseFileReader(new ShpFiles(path), false,
  10. Charset.forName("GBK"));
  11. DbaseFileHeader header = reader.getHeader();
  12. int numFields = header.getNumFields();
  13. for (int i = 0; i < numFields; i++) {
  14. header.getFieldName(i);
  15. header.getFieldType(i);// 'C','N'
  16. header.getFieldLength(i);
  17. }
  18. // 迭代读取记录
  19. while (reader.hasNext()) {
  20. try {
  21. Object[] entry = reader.readEntry();
  22. for (int i = 0; i < numFields; i++) {
  23. String title = header.getFieldName(i);
  24. Object value = entry[i];
  25. String name = title.toString(); // column
  26. String info = value.toString(); // value
  27. }
  28. } catch (Exception e) {
  29. e.printStackTrace();
  30. }
  31. }
  32. } catch (Exception ex) {
  33. ex.printStackTrace();
  34. } finally {
  35. if (reader != null) {
  36. // 关闭
  37. try {
  38. reader.close();
  39. } catch (Exception e) {
  40. }
  41. }
  42. }
  43. }

输出一个shp文件

  1. /**
  2. * 创建shp文件
  3. *
  4. * @param outPath
  5. */
  6. public void createShp(String outPath) {
  7. try {
  8. // 定义属性
  9. final SimpleFeatureType TYPE = DataUtilities.createType("Location",
  10. "location:Point," + "NAME:String," + "INFO:String,"
  11. + "OWNER:String");
  12. FeatureCollection<SimpleFeatureType, SimpleFeature> collection = FeatureCollections.newCollection();
  13. GeometryFactory geometryFactory = new GeometryFactory();
  14. SimpleFeatureBuilder featureBuilder = new SimpleFeatureBuilder(TYPE);
  15. double latitude = Double.parseDouble("116.123456789");
  16. double longitude = Double.parseDouble("39.120001");
  17. String NAME = "运通110路";
  18. String INFO = "白班车,学生票有效";
  19. String OWNER = "001";
  20. //创建坐标
  21. Point point = geometryFactory.createPoint(new Coordinate(longitude,latitude));
  22. //创建属性值
  23. Object[] obj = {point, NAME, INFO, OWNER };
  24. //构造一个Feature
  25. SimpleFeature feature = featureBuilder.buildFeature(null, obj);
  26. //添加到集合
  27. collection.add(feature);
  28. // shap文件的输出路径
  29. File newFile = new File(outPath);
  30. Map<String, Serializable> params = new HashMap<String, Serializable>();
  31. params.put("url", (Serializable) newFile.toURI().toURL());
  32. params.put("create spatial index", (Serializable) Boolean.TRUE);
  33. ShapefileDataStoreFactory dataStoreFactory = new ShapefileDataStoreFactory();
  34. ShapefileDataStore newDataStore = (ShapefileDataStore) dataStoreFactory
  35. .createNewDataStore(params);
  36. newDataStore.createSchema(TYPE);
  37. newDataStore.setStringCharset(Charset.forName("GBK"));
  38. newDataStore.forceSchemaCRS(DefaultGeographicCRS.WGS84);
  39. String typeName = newDataStore.getTypeNames()[0];
  40. ShapefileFeatureLocking featureSource = (ShapefileFeatureLocking) newDataStore
  41. .getFeatureSource(typeName);
  42. // 创建一个事务
  43. Transaction transaction = new DefaultTransaction("create");
  44. featureSource.setTransaction(transaction);
  45. try {
  46. featureSource.addFeatures(collection);
  47. // 提交事务
  48. transaction.commit();
  49. } catch (Exception problem) {
  50. problem.printStackTrace();
  51. transaction.rollback();
  52. } finally {
  53. transaction.close();
  54. }
  55. } catch (Exception e) {
  56. e.printStackTrace();
  57. }
  58. }

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的更多相关文章

  1. GeoTools介绍、环境安装、读取shp文件并显示

    GeoTools是一个开放源代码(LGPL)Java代码库,它提供了符合标准的方法来处理地理空间数据,例如实现地理信息系统(GIS).GeoTools库实现了开放地理空间联盟(OGC)规范. Geot ...

  2. Spring-Boot ☞ ShapeFile文件读写工具类+接口调用

    一.项目目录结构树 二.项目启动 三.往指定的shp文件里写内容 (1) json数据[Post] { "name":"test", "path&qu ...

  3. JAVA用geotools读写shape格式文件

    转自:http://toplchx.iteye.com/blog/1335007 JAVA用geotools读写shape格式文件 (对应geotools版本:2.7.2) (后面添加对应geotoo ...

  4. geotools导入shp文件到Oracle数据库时表名带下划线的问题解决

    问题: 最近在做利用geotools导入shp文件到Oracle表中,发现一个问题Oracle表名带下划线时导入失败,问题代码行: dsOracle.getFeatureWriterAppend(or ...

  5. maven构建geotools应用工程

    前置条件 jdk1.7+eclipse+maven POM配置 <project xmlns="http://maven.apache.org/POM/4.0.0" xmln ...

  6. 简析服务端通过geotools导入SHP至PG的方法

    文章版权由作者李晓晖和博客园共有,若转载请于明显处标明出处:http://www.cnblogs.com/naaoveGIS/ 1.背景 项目中需要在浏览器端直接上传SHP后服务端进行数据的自动入PG ...

  7. geotools中泰森多边形的生成

    概述 本文讲述如何在geotools中生成泰森多边形,并shp输出. 泰森多边形 1.定义 泰森多边形又叫冯洛诺伊图(Voronoi diagram),得名于Georgy Voronoi,是由一组由连 ...

  8. 说说geotools中坐标转换那点事

    概述: 本文说说geotools中坐标转换的那点事情,以WGS84和web墨卡托相互转换为例. 效果: 转换前 转换后 单个Geometry转换 实现代码: package com.lzugis.ge ...

  9. geotools修改shapefile 属性名乱码问题

    在GeoServer中文社区的讨论地址为:http://opengeo.cn/bbs/read.php?tid=1701&page=e&#a 使用geotools修改shapefile ...

随机推荐

  1. 四、使用Maven和使用Eclipse构建javaWeb项目

    环境前边已经搭建过了,我们就再弄了. 1.使用Maven构建javaWeb项目 (1).键入以下命令: $ mvn archetype:generate -DgroupId=com.holytax.w ...

  2. (转)iOS学习之 plist文件的读写

    在做iOS开发时,经常用到到plist文件, 那plist文件是什么呢? 它全名是:Property List,属性列表文件,它是一种用来存储串行化后的对象的文件.属性列表文件的扩展名为.plist ...

  3. APUE学习笔记-一些准备

    从开始看APUE已经有快一个星期了,由于正好赶上这几天清明节放假,难得有了三天空闲假期可以不受打扰的学习APUE,现在已经看完前六章了,里面的大部分例程也都亲自编写,调试过了.但总觉得这样学过就忘,因 ...

  4. docker的基本使用

    docker 基本的使用命令docker pull centos:latestdocker images centosdocker run -i -t centos /bin/bash //也可以使用 ...

  5. mysql 事务类型表的用法

    mysql关联表(references)的条件:1.两个表必须是 InnoDB表类型2.使用在外键关系的域必须为索引型(Index)3.使用外键关系的域必须与数据类型相似 以下是父表和子表的例子:创建 ...

  6. DevExpress控件-- Gridcontrol合并表头

    写在前面的话: 在园子里逛了有一段时间了,一直想写点东西,但苦于自己的水平有限,生怕写出来的东西浪费了读者的时间.楼主有幸参加了公司DevExpress控件的培训,独乐乐不如众乐乐,特附上Demo以飨 ...

  7. 使用Fiddler提高前端工作效率 (介绍篇)

    1. Fiddler 是什么? Fiddler是用C#编写的一个免费的HTTP/HTTPS网络调试器.英语中Fiddler是小提琴的意思,Fiddler Web Debugger就像小提琴一样,可以让 ...

  8. php不允许用户提交空表单(php空值判断)

    我们在设计提交空的评论时依然可以写入数据库,并在页面显示出来.这显然是不合理的,所以需要我们加入空值判断 可以修改代码,添加些判断: 复制代码代码如下:   if(empty($_POST['name ...

  9. 跨域Ajax请求WebService方法

    一.允许跨域Ajax请求,更改如下配置: 在要调用的WebService上面添加特性标签: 二.以如下返回用户信息的WebService方法为例 三.在另一个网站上通过Ajax访问webService ...

  10. NCPC 2012 Bread Sorting

    逆序对数的应用: 逆序对数的写法有,二分,树状数组,分治: 学习一下: 树状数组版: 代码: #include<cstdio> #include<cstring> #inclu ...