shape文件至少包含shp、dbf、shx文件,否则会报错。shp文件存储地理空间信息;dbf文件存储属性信息;shx文件存储索引信息。
注意:关于import org.locationtech.jts.geom.Geometry和import com.vividsolutions.jts.geom.Geometry前者是新版本所采用的JTS,后者是老版本采用的JTS,你要采用像geotools的20版本,用前者;用geotool的18版本以前的用后者。否则会报错。
1,读取shape文件:
public static void readShapefile() throws IOException{
String shapefile = "E:\\supermap_data\\lalala\\my.shp";
File file = new File(shapefile);
// 申明一个存储空间
ShapefileDataStore shapDataStore = null;
// 将文件读取到工作空间
shapDataStore = new ShapefileDataStore(file.toURL());
// 设置编码,防止中文乱码
Charset charset = Charset.forName("GBK");
shapDataStore.setCharset(charset);
// 获取图层名称
String typeName = shapDataStore.getTypeNames()[0];
System.out.println(typeName);
SimpleFeatureSource featureSource = null;
// 根据图层名称来获取要素的source
featureSource = shapDataStore.getFeatureSource(typeName);
// 获取要素集
SimpleFeatureCollection result = featureSource.getFeatures();
// 获取要素集合,方便进行迭代读取每一个要素
SimpleFeatureIterator iterator = result.features();
while (iterator.hasNext()){
// 获取每一个要素
SimpleFeature feature = iterator.next();
System.out.println(feature.getID() + " " + feature.getDefaultGeometry());
}
}
2,写入shape文件
public static void writeShapefile() throws SchemaException, IOException {
String path="E:\\supermap_data\\lalala\\my.shp";
//1.创建shape文件对象
File file =new File(path);
Map params = new HashMap();
//用于捕获参数需求的数据类
//URLP:url to the .shp file.
params.put(ShapefileDataStoreFactory.URLP.key, file.toURI().toURL());
//2.创建一个新的数据存储——对于一个还不存在的文件。
ShapefileDataStore ds = (ShapefileDataStore) new ShapefileDataStoreFactory().createNewDataStore(params);
//3.定义图形信息和属性信息
//SimpleFeatureTypeBuilder 构造简单特性类型的构造器
SimpleFeatureTypeBuilder tBuilder = new SimpleFeatureTypeBuilder();
//设置
//WGS84:一个二维地理坐标参考系统,使用WGS84数据
// tBuilder.setCRS(DefaultGeographicCRS.WGS84);
// 需要添加 gt-epsg-hsql 架包
tBuilder.setSRS( "EPSG:4490" );
tBuilder.setName("shapefile");
//添加 一个点
tBuilder.add("the_geom", Point.class);
//添加一个id
tBuilder.add("osm_id", Long.class);
//添加名称
tBuilder.add("name", String.class);
//添加描述
tBuilder.add("des", String.class);
//设置此数据存储的特征类型
ds.createSchema(tBuilder.buildFeatureType());
//设置编码
ds.setCharset(Charset.forName("UTF-8"));
FeatureWriter writer = ds.getFeatureWriter(ds.getTypeNames()[0], Transaction.AUTO_COMMIT);
SimpleFeature feature = writer.next();
double x = 116.123; //X轴坐标
double y = 39.345 ; //Y轴坐标
Coordinate coordinate = new Coordinate(x, y);
Point point = new GeometryFactory().createPoint(coordinate);
feature.setAttribute("the_geom",point);
feature.setAttribute("osm_id", 1234567890l);
feature.setAttribute("name", "帅哥");
feature.setAttribute("des", "爱美女");
feature = writer.next();
x = 116.456;
y = 39.678 ;
coordinate = new Coordinate(x, y);
point = new GeometryFactory().createPoint(coordinate);
feature.setAttribute("the_geom",point);
feature.setAttribute("osm_id", 1234567891l);
feature.setAttribute("name", "美女");
feature.setAttribute("des", "爱帅哥");
writer.write();
writer.close();
ds.dispose();
}
3,使用的是geotools-20.2,maven配置文件如下:
UTF-8
20.2
org.geotools
gt-shapefile
${geotools.version}
org.geotools
gt-swing
${geotools.version}
org.geotools
gt-geojson
${geotools.version}
org.geotools
gt-main
${geotools.version}
org.geotools.jdbc
gt-jdbc-postgis
${geotools.version}
org.geotools
gt-epsg-hsql
${geotools.version}
osgeo
Open Source Geospatial Foundation Repository
http://download.osgeo.org/webdav/geotools/
true
opengeo
OpenGeo Maven Repository
http://repo.opengeo.org
参考博客:
1,Java+GeoTools工具包+读写shapfile文件
https://www.giserdqy.com/secdev/geotools/27801/
好的博文:
1,PostGIS的安装与初步使用
https://blog.csdn.net/qq_35732147/article/details/81169961
2,Windows环境下GeoTools 连接PostGIS时候一些坑
https://blog.csdn.net/chajizhuo3103/article/details/80353056
3,geotools中的空间关系(Geometry Relationships)和空间操作(Geometry Operations)
https://blog.csdn.net/gisshixisheng/article/details/56302250
4,geotools实现两个shp的相交计算
https://www.jianshu.com/p/08c78e00b7a8
5,postgis与geotools对应方法总结
https://blog.csdn.net/lnxyangruosong/article/details/80888808
6,GeoTools应用-JTS(Geometry之间的关系)
https://blog.csdn.net/sxausgyy/article/details/8113077
7,Geometry 点线面生成方式
https://www.iteye.com/blog/491569462-qq-com-2071793