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

  1. GeoJson的生成与解析,JSON解析,Java读写geojson,geotools读取shp文件,Geotools中Geometry对象与GeoJson的相互转换

    GeoJson的生成与解析 一.wkt格式的geometry转成json格式 二.json格式转wkt格式 三.json格式的数据进行解析 四.Java读写geojson 五.geotools读取sh ...

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

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

  3. python GDAL 读写shp文件

    gdal包用于处理栅格数据,ogr用于处理矢量数据. 1 #!C:\Program Files\pythonxy\python\python.exe 2 #-*- coding:gb2312 -*- ...

  4. C#、C++用GDAL读shp文件(转载)

    C#.C++用GDAL读shp文件 C#用GDAL读shp文件 (2012-08-14 17:09:45) 标签: 杂谈 分类: c#方面的总结 1.目前使用开发环境为VS2008+GDAL1.81 ...

  5. Android GIS开发系列-- 入门季(13)Gdal简单写个shp文件

    Gdal是用来读写栅格与矢量数据的,在Gdal官网,可以下载相关的资源进行平台的编译.其实Arcgis底层也是用Gdal来读取shp文件的,那在Android中可以直接读写shp文件吗,是可以的.这里 ...

  6. shp系列(一)——利用C++进行shp文件的读(打开)与写(创建)开言

    博客背景和目的 最近在用C++写一个底层的东西,需要读取和创建shp文件.虽然接触shp文件已经几年了,但是对于shp文件内到底包含什么东西一直是一知半解.以前使用shp文件都是利用软件(如ArcGI ...

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

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

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

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

  9. GDAL1.11版本号对SHP文件索引加速測试

    GDAL库中对于矢量数据的读取中能够设置一些过滤器来对矢量图形进行筛选.对于Shapefile格式来说.假设数据量太大,设置这个过滤器时间慢的简直无法忍受.好在GDAL1.10版本号開始支持读取Sha ...

随机推荐

  1. bzoj3997组合数学(求最长反链的dp)

    组合数学 给出一个网格图,其中某些格子有财宝,每次从左上角出发,只能向下或右走.问至少走多少次才能将财宝捡完.此对此问题变形,假设每个格子中有好多财宝,而每一次经过一个格子至多只能捡走一块财宝,至少走 ...

  2. Crossin 8-3;8-4

    8-3文件打开模式:r:只读模式.默认w:只写模式.会先清空文件a:追加写入模式,在文件末尾写入,不可读r+:打开一个文件用于读写.文件指针将会放在文件的开头,原文件内容不会清空b:二进制模式,与前面 ...

  3. IntelliJ IDEA 14.1.4设置关闭自动保存和标志改动文件为星号?

    一.设置取消默认的自动保存: 二.设置文件内容改动后为文件标题头部添加*号:

  4. ubuntu中切换到root账号方法

    ubuntu如何切换到root用户身份? 前面小编分享了ubuntu怎么开启root用户,下面小编来分享如何切到到root用户 方法/步骤 按照下面的方式打开终端工具,或者使用终端工具的快捷键Ctrl ...

  5. Python之scrapy安装

    1.按照网上教程一步步实验,运行时报错: 'HtmlResponse' object has no attribute 'xpath' in scrapy 个人使用的是scrapy0.14.4,搜索得 ...

  6. ConvertUtils.register注册转换器

    当用到BeanUtils的populate.copyProperties方法或者getProperty,setProperty方法其实都会调用convert进行转换 但Converter只支持一些基本 ...

  7. vue + ajax + php 接口的使用小接

    vue + ajax + php 接口的使用小接 前端代码: (获取用户信息,并渲染页面) userinfor.html <!DOCTYPE html> <html lang=&qu ...

  8. WebApi接口返回json,xml,text纯文本等

    [Route("api/Message/MessageList/")] [HttpGet] public HttpResponseMessage MessageList() { R ...

  9. Sqlserver中存储过程和游标的一些使用例子

    /*带输入输出参数存储过程*/ ALTER PROCEDURE pro_test2 @userID INT, @maxUserID INT OUTPUT, @countUser INT OUTPUT ...

  10. JSONP、图片Ping、XMLHttpRequest2.0等跨域资源请求(CORS)

    跨域:当协议.主域名.子域名.端口号中任意一个不相同时都不算同一个域,而在不同域之间请求数据即为跨域请求.解决方法有以下几种(如有错误欢迎指出)以请求图片url为例: 1.通过XMLHttpReque ...