通过geotools读写shp文件
依赖jar包

读取shp
public static void main(String[] path) {
DbaseFileReader reader = null;
try {
reader = new DbaseFileReader(new ShpFiles("C:\\Users\\lilei3774\\Desktop\\wuhan\\1\\split-hanzi\\poi.shp"), false,
Charset.forName("utf-8"));
DbaseFileHeader header = reader.getHeader();
int numFields = header.getNumFields();
while (reader.hasNext()) {
try {
Object[] entry = reader.readEntry();
for (int i = 0; i < numFields; i++) {
String title = header.getFieldName(i);
Object value = entry[i];
System.out.println(title + "=" + value);
}
} catch (Exception e) {
e.printStackTrace();
}
}
} catch (Exception e) {
e.printStackTrace();
} finally {
if (reader != null) {
try {
reader.close();
} catch (Exception e) {
}
}
}
}
public static void main(String[] args) {
// TODO Auto-generated method stub
ShapefileDataStore shpDataStore = null;
try{
shpDataStore = new ShapefileDataStore(new File("C:\\Users\\lilei3774\\Desktop\\wuhan\\1\\split-hanzi\\poi.shx").toURI().toURL());
// shpDataStore.setStringCharset(Charset.forName("GBK"));
shpDataStore.setCharset(Charset.forName("utf-8"));
String typeName = shpDataStore.getTypeNames()[0];
FeatureSource<SimpleFeatureType, SimpleFeature> featureSource = null;
featureSource = (FeatureSource<SimpleFeatureType, SimpleFeature>)shpDataStore.getFeatureSource(typeName);
FeatureCollection<SimpleFeatureType, SimpleFeature> result = featureSource.getFeatures();
System.out.println(result.size());
FeatureIterator<SimpleFeature> itertor = result.features();
while(itertor.hasNext()){
SimpleFeature feature = itertor.next();
Collection<Property> p = feature.getProperties();
Iterator<Property> it = p.iterator();
while(it.hasNext()) {
Property pro = it.next();
if (pro.getValue() instanceof Point) {
System.out.println("PointX = " + ((Point)(pro.getValue())).getX());
System.out.println("PointY = " + ((Point)(pro.getValue())).getY());
} else {
System.out.println(pro.getName() + " = " + pro.getValue());
}
}
}
itertor.close();
} catch (MalformedURLException e) {
e.printStackTrace();
} catch(IOException e) { e.printStackTrace(); }
}
写shp
public static void main(String[] args) {
try{
File file = new File("C:\\Users\\lilei3774\\Desktop\\wuhan\\1\\split-hanzi\\self.shp");
Map<String, Serializable> params = new HashMap<String, Serializable>();
params.put( ShapefileDataStoreFactory.URLP.key, file.toURI().toURL() );
ShapefileDataStore ds = (ShapefileDataStore) new ShapefileDataStoreFactory().createNewDataStore(params);
//定义图形信息和属性信息
SimpleFeatureTypeBuilder tb = new SimpleFeatureTypeBuilder();
tb.setCRS(DefaultGeographicCRS.WGS84);
tb.setName("shapefile");
tb.add("the_geom", Point.class);
tb.add("POIID", Long.class);
tb.add("NAMEC", String.class);
ds.createSchema(tb.buildFeatureType());
ds.setCharset(Charset.forName("utf-8"));
//设置Writer
FeatureWriter<SimpleFeatureType, SimpleFeature> writer = ds.getFeatureWriter(ds.getTypeNames()[0], Transaction.AUTO_COMMIT);
//写下一条
SimpleFeature feature = writer.next();
feature.setAttribute("the_geom", new GeometryFactory().createPoint(new Coordinate(116.123, 39.345)));
feature.setAttribute("POIID", 1234567890l);
feature.setAttribute("NAMEC", "某兴趣点1");
feature = writer.next();
feature.setAttribute("the_geom", new GeometryFactory().createPoint(new Coordinate(116.456, 39.678)));
feature.setAttribute("POIID", 1234567891l);
feature.setAttribute("NAMEC", "某兴趣点2");
writer.write();
writer.close();
ds.dispose();
} catch (Exception e) {
e.printStackTrace();
}
}
通过geotools读写shp文件的更多相关文章
- GeoJson的生成与解析,JSON解析,Java读写geojson,geotools读取shp文件,Geotools中Geometry对象与GeoJson的相互转换
GeoJson的生成与解析 一.wkt格式的geometry转成json格式 二.json格式转wkt格式 三.json格式的数据进行解析 四.Java读写geojson 五.geotools读取sh ...
- geotools导入shp文件到Oracle数据库时表名带下划线的问题解决
问题: 最近在做利用geotools导入shp文件到Oracle表中,发现一个问题Oracle表名带下划线时导入失败,问题代码行: dsOracle.getFeatureWriterAppend(or ...
- python GDAL 读写shp文件
gdal包用于处理栅格数据,ogr用于处理矢量数据. 1 #!C:\Program Files\pythonxy\python\python.exe 2 #-*- coding:gb2312 -*- ...
- C#、C++用GDAL读shp文件(转载)
C#.C++用GDAL读shp文件 C#用GDAL读shp文件 (2012-08-14 17:09:45) 标签: 杂谈 分类: c#方面的总结 1.目前使用开发环境为VS2008+GDAL1.81 ...
- Android GIS开发系列-- 入门季(13)Gdal简单写个shp文件
Gdal是用来读写栅格与矢量数据的,在Gdal官网,可以下载相关的资源进行平台的编译.其实Arcgis底层也是用Gdal来读取shp文件的,那在Android中可以直接读写shp文件吗,是可以的.这里 ...
- shp系列(一)——利用C++进行shp文件的读(打开)与写(创建)开言
博客背景和目的 最近在用C++写一个底层的东西,需要读取和创建shp文件.虽然接触shp文件已经几年了,但是对于shp文件内到底包含什么东西一直是一知半解.以前使用shp文件都是利用软件(如ArcGI ...
- JAVA用geotools读写shape格式文件
转自:http://toplchx.iteye.com/blog/1335007 JAVA用geotools读写shape格式文件 (对应geotools版本:2.7.2) (后面添加对应geotoo ...
- 简析服务端通过geotools导入SHP至PG的方法
文章版权由作者李晓晖和博客园共有,若转载请于明显处标明出处:http://www.cnblogs.com/naaoveGIS/ 1.背景 项目中需要在浏览器端直接上传SHP后服务端进行数据的自动入PG ...
- GDAL1.11版本号对SHP文件索引加速測试
GDAL库中对于矢量数据的读取中能够设置一些过滤器来对矢量图形进行筛选.对于Shapefile格式来说.假设数据量太大,设置这个过滤器时间慢的简直无法忍受.好在GDAL1.10版本号開始支持读取Sha ...
随机推荐
- 【转】NAS群晖DSM 5.0-4458安装教程
需要准备: 1.一个闲置的U盘,容量大于64M即可. 墙裂建议用如下U盘,可以隐藏成灰群晖,小巧方便. 闪迪(SanDisk)酷豆(CZ33)8GB U盘¥29.9京东商城 2.一台显示器,用于 ...
- webpackJsonp is not defined
解决方法公共模块放前面
- 轻松几句搞定【Javascript中的this指向】问题
this关键字在JavaScript中扮演了至关重要的角色,每次它的出现都伴随着它的指向问题,这也是很多初学者容易出错的地方. 不过,这篇文章将会带你一次性搞定this指向的问题,望能给大家提供帮助! ...
- 国内为什么没有好的 Stack Overflow 的模仿者?,因为素质太低?没有分享精神?
今天终于在下班前搞定一个技术问题,可以准时下班啦.当然又是通过StackOverflow找到的解决思路,所以下班路上和同事顺便聊起了它,两个资深老程序猿,还是有点感叹,中国的程序员群体人数应该不少,为 ...
- 超全面!这可能是最全面的 jQuery 知识总结
个人建议:学习 jQuery 前先掌握基本的 JavaScrpit 语法,特别是对函数要掌握,jQuery 基本上是使用函数. jQuery 简介 jQuery 是一个轻量级 JavaScript 库 ...
- 【特效】几种实用的按钮hover效果
效果预览:http://www.gbtags.com/gb/rtreplayerpreview-standalone/3095.htm html: <ul class="btn&quo ...
- CSS之 absoulte 属性
特性: absoulte 与 float 具有相同的特性:包裹性,与破坏性 absoulte 与 float 可以交替使用 不受 relative 限制的 absoulte 定位,行为表现上可以不 ...
- win10 uwp 使用 Geometry resources 在 xaml
经常会遇到在 xaml 使用矢量图,对于 svg 的矢量图,一般都可以拿出来写在 Path 的 Data ,所以可以写为资源,但是写出来的是字符串,如何绑定 Geometry 到字符串资源? 假如在资 ...
- win10 uwp 打电话
UWP可以使用打电话功能,在PC是用Skype,在手机是直接使用电话功能. UWP可以通过Skype打电话,那么如何通过应用间通讯,很简单使用Launcher. Skype电话使用Skype:(电话号 ...
- git 合并两个仓库
我有两个仓库,一个是gitbook在写一本 一个是放在github的垃圾,这个是我想要开个人网站,但是做的还是不行https://github.com/lindexi/lindexi.github.i ...