World Wind Java开发之六——解析shape文件(转)
http://blog.csdn.net/giser_whu/article/details/41647117
最近一直忙于导师项目的事情了,几天没更新了,昨天和今天研究了下WWJ解析shp文件的源代码,现在记录下,希望可以帮到更多的人!
上一篇博客:World Wind Java开发之五——读取本地shp文件只讲了如何加载shp文件,没有涉及到shp文件的解析,我们这篇博客紧接上一篇博客,利用WWJ来解析shp文件。首先来看用到的源码包和相关类,如下图所示。解析shp文件主要用到Shapefile(shapefile文件类)、ShapefileRecord(shape文件记录类)、DBaseRecord类以及DBaseField(字段类)
1、读取shapefile文件
- /**
- * Opens a Shapefile from an InputStream, and InputStreams to its optional
- * resources.
- * <p/>
- * The source Shapefile may be accompanied optional streams to an index
- * resource stream, an attribute resource stream, and a projection resource
- * stream. If any of these streams are null or cannot be read for any
- * reason, the Shapefile opens without that information.
- * <p/>
- * This throws an exception if the shapefile's coordinate system is
- * unsupported.
- *
- * @param shpStream
- * the shapefile geometry file stream.
- * @param shxStream
- * the index file stream, can be null.
- * @param dbfStream
- * the attribute file stream, can be null.
- * @param prjStream
- * the projection file stream, can be null.
- * @throws IllegalArgumentException
- * if the shapefile geometry stream <code>shpStream</code> is
- * null.
- * @throws WWRuntimeException
- * if the shapefile cannot be opened for any reason, or if the
- * shapefile's coordinate system is unsupported.
- */
- public Shapefile(InputStream shpStream, InputStream shxStream,
- InputStream dbfStream, InputStream prjStream)
- {
- this(shpStream, shxStream, dbfStream, prjStream, null);
- }
输入文件流分别对应着.shp .shx .dbf .prj文件
- /**
- * Opens an Shapefile from a general source. The source type may be one of
- * the following:
- * <ul>
- * <li>{@link java.io.InputStream}</li>
- * <li>{@link java.net.URL}</li>
- * <li>{@link File}</li>
- * <li>{@link String} containing a valid URL description or a file or
- * resource name available on the classpath.</li>
- * </ul>
- * <p/>
- * The source Shapefile may be accompanied by an optional index file,
- * attribute file, and projection file. To be recognized by this Shapefile,
- * accompanying files must be in the same logical folder as the Shapefile,
- * have the same filename as the Shapefile, and have suffixes ".shx",
- * ".dbf", and ".prj" respectively. If any of these files do not exist, or
- * cannot be read for any reason, the Shapefile opens without that
- * information.
- * <p/>
- * This throws an exception if the shapefile's coordinate system is
- * unsupported, or if the shapefile's coordinate system is unsupported.
- *
- * @param source
- * the source of the shapefile.
- * @throws IllegalArgumentException
- * if the source is null or an empty string.
- * @throws WWRuntimeException
- * if the shapefile cannot be opened for any reason.
- */
- public Shapefile(Object source)
- {
- this(source, null);
- }
这种方法秩序给出shp文件的路径即可,但是若只有shp文件,缺少shx等文件则无法解析shape文件。
- String shpFilePath = "D:\\Users\\wwj_data\\states.shp";
- String shxFilePath = "D:\\Users\\wwj_data\\states.shx";
- String dbfFilePath = "D:\\Users\\wwj_data\\states.dbf";
- String prjFilePath = "D:\\Users\\wwj_data\\states.prj";
- Shapefile shapefile = new Shapefile(shpFilePath);
- System.out.println(shapefile.getShapeType());
或者:---------------------------------------------------------------------------------------
- String shpFilePath = "D:\\Users\\wwj_data\\states.shp";
- String shxFilePath = "D:\\Users\\wwj_data\\states.shx";
- String dbfFilePath = "D:\\Users\\wwj_data\\states.dbf";
- String prjFilePath = "D:\\Users\\wwj_data\\states.prj";
- InputStream shpInputStream = new FileInputStream(shpFilePath);
- InputStream shxInputStream = new FileInputStream(shxFilePath);
- InputStream dbfInputStream = new FileInputStream(dbfFilePath);
- InputStream prjInputStream = new FileInputStream(prjFilePath);
- // 实例化一个shapefile类
- Shapefile shapefile = new Shapefile(shpInputStream, shxInputStream,
- dbfInputStream, prjInputStream);
- System.out.println(shapefile.getShapeType()); // shape类型
2、获取shapefile文件的属性表信息
- protected DBaseFile attributeFile;
- /**
- *
- * @方法名称: getAttributesTable ;
- * @方法描述: 获取属性表 ;
- * @参数 :@return
- * @返回类型: DBaseFile ;
- * @创建人:奔跑的鸡丝 ;
- * @创建时间:2014-12-1 下午12:55:33;
- * @throws
- */
- public DBaseFile getAttributesTable()
- {
- return this.attributeFile;
- }
获取属性表后,首先要获取属性表的基本信息,如:shape文件的类型、字段个数以及记录个数。另外输出所有字段名
- // 获取shp属性表
- DBaseFile dBaseFile = shapefile.getAttributesTable();
- int fieldCount = dBaseFile.getNumberOfFields(); // 字段数
- int recordsCount = dBaseFile.getNumberOfRecords(); // 记录数
- System.out.println("字段数为:" + fieldCount);
- System.out.println("记录数为:" + recordsCount);
- System.out.println(shapefile.getShapeType()); // shape类型
- //获取字段集合
- DBaseField [] dBaseFields=dBaseFile.getFields();
- for (int i = 0; i < fieldCount; i++)
- {
- System.out.println(dBaseFields[i].getName());
- }
运行结果如下:
3、获取字段值
- // 解析shape文件
- try
- {
- while (shapefile.hasNext())
- {
- ShapefileRecord record = shapefile.nextRecord(); // 获取一条记录
- DBaseRecord dBaseRecord = record.getAttributes(); // 获取该记录的属性信息
- Object[] values = dBaseRecord.getValues().toArray();//获取字段值集合
- for (int i = 0; i < values.length; i++)
- {
- System.out.println(values[i].toString());
- }
- System.out.println("------------------");
- }
- }
- catch (Exception e)
- {
- e.printStackTrace();
- System.out.println("解析shapefile文件出错!");
- }
- finally
- {
- WWIO.closeStream(shapefile, shpFilePath);
- WWIO.closeStream(shapefile, shxFilePath);
- WWIO.closeStream(shapefile, dbfFilePath);
- WWIO.closeStream(shapefile, prjFilePath);
- }
思路很简单:shapefile文件—>获取一条记录—>获取记录的属性信息-->获取字段值集合。但是有一个问题:不支持中文字段值
- /**
- * @方法名称: shapeFileReader ;
- * @方法描述: 读取sh文件 ;
- * @参数 :@throws FileNotFoundException
- * @返回类型: void ;
- * @创建人:奔跑的鸡丝 ;
- * @创建时间:2014-12-1 下午12:50:11;
- * @throws
- */
- private void shapeFileReader() throws FileNotFoundException
- {
- String shpFilePath = "D:\\Users\\wwj_data\\states.shp";
- String shxFilePath = "D:\\Users\\wwj_data\\states.shx";
- String dbfFilePath = "D:\\Users\\wwj_data\\states.dbf";
- String prjFilePath = "D:\\Users\\wwj_data\\states.prj";
- InputStream shpInputStream = new FileInputStream(shpFilePath);
- InputStream shxInputStream = new FileInputStream(shxFilePath);
- InputStream dbfInputStream = new FileInputStream(dbfFilePath);
- InputStream prjInputStream = new FileInputStream(prjFilePath);
- // 实例化一个shapefile类
- Shapefile shapefile = new Shapefile(shpInputStream, shxInputStream,
- dbfInputStream, prjInputStream);
- // 获取shp属性表
- DBaseFile dBaseFile = shapefile.getAttributesTable();
- int fieldCount = dBaseFile.getNumberOfFields(); // 字段数
- int recordsCount = dBaseFile.getNumberOfRecords(); // 记录数
- System.out.println("字段数为:" + fieldCount);
- System.out.println("记录数为:" + recordsCount);
- System.out.println(shapefile.getShapeType()); // shape类型
- //获取字段集合
- DBaseField [] dBaseFields=dBaseFile.getFields();
- for (int i = 0; i < fieldCount; i++)
- {
- System.out.print(dBaseFields[i].getName()+" ");
- }
- System.out.println();
- // 解析shape文件
- try
- {
- while (shapefile.hasNext())
- {
- ShapefileRecord record = shapefile.nextRecord(); // 获取一条记录
- DBaseRecord dBaseRecord = record.getAttributes(); // 获取该记录的属性信息
- Object[] values = dBaseRecord.getValues().toArray();//获取字段值集合
- for (int i = 0; i < values.length; i++)
- {
- System.out.print(values[i].toString()+" ");
- }
- System.out.println("------------------");
- }
- }
- catch (Exception e)
- {
- e.printStackTrace();
- System.out.println("解析shapefile文件出错!");
- }
- finally
- {
- WWIO.closeStream(shapefile, shpFilePath);
- WWIO.closeStream(shapefile, shxFilePath);
- WWIO.closeStream(shapefile, dbfFilePath);
- WWIO.closeStream(shapefile, prjFilePath);
- }
- }
-----------------------------------------------华丽的分割线----------------------------------------------
- while (shapefile.hasNext())
- {
- ShapefileRecord record = shapefile.nextRecord(); // 获取一条记录
- DBaseRecord dBaseRecord = record.getAttributes(); // 获取该记录的属性信息
- ArrayList<String> fieldArrayList = new ArrayList<String>();
- for (int i = 0; i < fieldCount; i++)
- {
- /**
- * 根据字段名称来获取字段值
- */
- String fieldValue = dBaseRecord.getValue(
- dBaseFields[i].getName()).toString(); //
- fieldArrayList.add(fieldValue);
- System.out.print(fieldValue + " ");
- }
- System.out.println("------------------");
- }
执行结果如下图所示。
修改 gov.nasa.worldwind.formats.shapefile.DBaseFile;下的
decodeString 方法中的UTF-8 类型改为 GBK类型,希望对您有用
World Wind Java开发之六——解析shape文件(转)的更多相关文章
- [转]World Wind Java开发之四——搭建本地WMS服务器
在提供地理信息系统客户端时,NASA还为用户提供了开源的WMS Server 服务器应用:World Wind WMS Server.利用这个应用,我们可以架设自己的WMS服务并使用自己的数据(也支持 ...
- java jar包解析:打包文件,引入文件
java jar包解析:打包文件,引入文件 cmd下: jar命令:package包打包 javac命令:普通类文件打包 Hello.java: package org.lxh.demo; publi ...
- World Wind Java开发之一(转)
http://blog.csdn.net/giser_whu/article/details/40477235 参照<World wind Java三维地理信息系统开发指南随书光盘>以及官 ...
- [转]World Wind Java开发之五——读取本地shp文件
World Wind Java 使用IconLayer图层类表现点和多点数据,使用RenderableLayer图层表现线和面数据,一个图层只能对应一组shape文件.World Wind Java首 ...
- World Wind Java开发之五——读取本地shp文件(转)
http://blog.csdn.net/giser_whu/article/details/41484433 World Wind Java 使用IconLayer图层类表现点和多点数据,使用Ren ...
- JAVA使用SAX解析XML文件
在我的另一篇文章(http://www.cnblogs.com/anivia/p/5849712.html)中,通过一个例子介绍了使用DOM来解析XML文件,那么本篇文章通过相同的XML文件介绍如何使 ...
- Java 创建过滤器 解析xml文件
今天写了一个过滤器demo,现在是解析actions.xml文件,得到action中的业务规则:不需要导入任何jar包 ActionFilter过滤器类: package accp.com.xh.ut ...
- java使用dom4j解析xml文件
关于xml的知识,及作用什么的就不说了,直接解释如何使用dom4j解析.假如有如下xml: dom4j解析xml其实很简单,只要你有点java基础,知道xml文件.结合下面的xml文件和java代码, ...
- Java:JXL解析Excel文件
项目中,有需求要使用JXL解析Excel文件. 解析Excel文件 我们先要将文件转化为数据流inputStream. 当inputStream很大的时候 会造成Java虚拟器内存不够 抛出内存溢出 ...
随机推荐
- Python数据聚合和分组运算(2)-Data Aggregation
在上一篇博客里我们讲解了在python里运用pandas对数据进行分组,这篇博客将接着讲解对分组后的数据进行聚合. 1.python 中经过优化的groupy方法 先读入本文要使用的数据集tips. ...
- python语言基础语法笔记<note1库安装和工具安装>
Python是一门入门简单的编程语言,它的安装和搭建也非常简单.在大部分的发行Linux版本上都预装了python2,部分也预装了python3,需要查看Linux上是否安装Python,只需要在 命 ...
- 初识XML
前言 前期敲机房的时候,总是写配置文件,后来到ASP.NET学习中也遇到了XML,可是什么是XML呢,它又是用来干什么的呢?请听我细细道来. 内容 定义 (eXtensible Markup Lang ...
- cf831D(dp)
题目链接: http://codeforces.com/contest/831/problem/D 题意: 有 n 个人和 k 把钥匙, 数组 a 为 n 个人的初始位置, 数组 b 为 k 把钥匙的 ...
- 【BZOJ4144】[AMPPZ2014]Petrol(最短路+最小生成树+并查集)
Description 给定一个n个点.m条边的带权无向图,其中有s个点是加油站. 每辆车都有一个油量上限b,即每次行走距离不能超过b,但在加油站可以补满. q次询问,每次给出x,y,b,表示出发点是 ...
- click点击事件先后顺序的问题
//页面加载时,每秒钟调用一次var times = setInterval("loadFlws()","1000"); function loadFlws() ...
- css3旋转立方体-_-
<!DOCTYPE html> <html> <head lang="en"> <meta charset="UTF-8&quo ...
- HDU2047 阿牛的EOF牛肉串
题目:https://blog.csdn.net/qq_40932661?t=1 表面上看去似乎无从下手.但是可以从前面地推出后面的 递推: 假如涂第N个位置,有两种可能,①涂O ②不涂O. 如果涂O ...
- POJ1028 Web Navigation
题目来源:http://poj.org/problem?id=1028 题目大意: 模拟实现一个浏览器的“前进”和“回退”功能.由一个forward stack和一个backward stack实现. ...
- LeetCode初级算法(树篇)
目录 二叉树的最大深度 验证二叉搜索树 对称二叉树 二叉树的层次遍历 将有序数组转换为二叉搜索树 二叉树的最大深度 二叉树,所以可以考虑用递归来做.由于根节点已经算过了,所以需要加上1:每次返回都是以 ...