使用GEOTools创建空shp文件

在地理信息中最常用的数据就是shp矢量数据,下面介绍使用GEOTools创建shp文件的大概步骤(这里考虑到创建要素的代码繁复,不容易看懂,所以先抽丝剥茧地把新建一个空的SHP文件的原理介绍一下):
1、设置SHP的文件属性
2、创建一个数据存储空间生成工厂
3、创建一个数据存储空间
4、通过数据存储空间生成带文件属性架构的SHP文件
具体代码如下:
import java.io.File;
import java.io.Serializable;
import java.util.HashMap;
import java.util.Map;
import org.geotools.data.DataUtilities;
import org.geotools.data.shapefile.ShapefileDataStore;
import
org.geotools.data.shapefile.ShapefileDataStoreFactory;
import org.opengis.feature.simple.SimpleFeatureType;
public class CopyOfNew 
{
   public static void
main(String[] args) throws Exception 
   {
     
  //指定SHP文件建立路径
     
  File newFile =new
File(“E:\\workspace.shp”);
     
  
     
  //设置要生成的shp文件的属性
     
  //下面是定义要素的字段(属性)
     
  //第一个参数是要素类型,第二个参数是字段名
     
 
//下面对应SHP文件的dbf表中的Shape、name和number字段,FID字段默认生成
     
 
//其中srid=4326是定义地理坐标系WGS_84,与ESRI的WKID一样,因为都是OGC定义的
     
  final SimpleFeatureType TYPE =
DataUtilities.createType(
     
  “Location”,
     
     
    “the_geom:Point:srid=4326,” +
// <- the geometry attribute: Point type
     
     
    “name:String,” +
  // <- a String attribute
     
     
    “number:Integer”
  // a number attribute
     
  );
     
  //SHP数据存储工厂
     
  ShapefileDataStoreFactory dataStoreFactory = new
ShapefileDataStoreFactory();
     
  //定义生成时的属性
     
  Map params = new HashMap();
     
  params.put(“url”,
newFile.toURI().toURL());
     
  params.put(“create spatial index”,
Boolean.TRUE);
     
  //生成SHP
     
  ShapefileDataStore newDataStore =
(ShapefileDataStore)
dataStoreFactory.createNewDataStore(params);
     
  newDataStore.createSchema(TYPE);
   }
以上注释有不对的地方多多指教!

转载自:https://blog.csdn.net/SCNU_Arain/article/details/84879704

You may also like...

退出移动版