依赖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. An Introduction to Variational Methods (5.2)

    我们现在已经得到了关于潜在变量Z的优化分布的表达形式: ‍ 其中: ‍ 所以现在我们可以得到Z的期望: ‍ 另外对于Z还值得一提的是,我们从其优化分布的表达式中可以看出,各个Z的组成部分之间还是相互耦 ...

  2. ServiceStack.Text / Newtonsoft.Json 两种json序列化性能比较

    JSON序列化现在应用非常多,尤其在前后端分离的情况下,平常大多数C#下都使用Newtonsoft.Json来操作,量少的情况下,还可以忽略,但量大的情况下就要考虑使用ServiceStack.Tex ...

  3. 将 Intent 序列化,像 Uri 一样传递 Intent!!!

    一.真的需要new一个Intent吗? 在 Android 中,打开一个 Activity ,有多少种方式?不过不管是使用什么方式,最终都没办法逃避创建一个 Intent ,然后startActivi ...

  4. c# 【MVC】WebApi设置返回Json

    public static HttpResponseMessage toJson(Object obj) { String str; if (obj is String || obj is Char) ...

  5. Sql Server合并多行询数据到一行:使用自连接、FOR XML PATH('')、STUFF或REPLACE函数

    示例表 tb 数据如下 id value ----- 1 aa 1 bb 2 aaa 2 bbb 2 ccc SELECT id, [val] = ( SELECT [value] + ',' FRO ...

  6. MYSQL数据库45道练习题

    --第一题查询Student表中的所有记录的Sname.Ssex和Class列.select Sname,Ssex,Class from Student;--第二题查询教师所有的单位即不重复的Depa ...

  7. PhoneWindow,ViewRoot,Activity之间的大致关系

    http://www.nowamagic.net/academy/detail/50160216 在android里,我们都知道activity.但是一个activity跟一个Window是一个什么关 ...

  8. linux 下查找图片文件方法

    通常是通过文件后缀名查找图片文件,如果没有文件后缀的图片或者伪造的图片文件,则这种判定方法将达不到要求.我们可以根据读取文件头进行图片文件类型的判定. 比较流行的图片文件类型有:jpg png bmp ...

  9. ubuntu下MySQL修改root密码的多种方法,phpmyadmin空密码无法登陆的解决方法

    phpmyadmin是默认不允许使用空密码的,所以若是在安装时没有设置密码,在登陆phpmyadmin时是个很头疼的问题 方法1是修改phpmyadmin的配置文件,这里不做推荐.. 方法2: php ...

  10. Java集合框架体系详细梳理,含面试知识点。

    一.集合类 集合的由来: 面向对象语言对事物都是以对象的形式来体现,为了方便对多个对象的操作,就需要将对象进行存储,集合就是存储对象最常用的一种方式. 集合特点: 1,用于存储对象的容器.(容器本身就 ...