shape文件结构

  • filename.shp: shapes
  • filename.shx: 索引文件
  • filename.dbf: 结构化数据文件
  • filename.qix: 空间索引文件
  • filename.fix: fid索引文件
  • filename.sld: 样式文件

依赖

    org.geotools    gt-main    27.2    org.geotools    gt-shapefile    27.2

创建连接连接参数

ParameterrequiredDescription
urltrue.shp文件的url
namespacefalseFeatureType的URI
create spatial indexfalse是否创建空间索引,默认true
charsetfalse解码DBF文件的编码,默认ISO_8859_1
timezonefalse解析DBF文件时间的时区
memory mapped bufferfalse内存映射,默认false
cache and reuse memory mapsfalse使用内存映射时,缓存并重用,默认true
enable spatial indexfalse是否使用空间索引,默认true

代码示例

/** *  * 创建shape文件 *  * */FileDataStoreFactorySpi factory = new ShapefileDataStoreFactory();//新建文件File file = new File("my.shp");//datastoreMap params = new HashMap();params.put("url", file.toURI().toURL());params.put("create spatial index", Boolean.TRUE);DataStore dataStore = factory.createNewDataStore(params);//Feature数据定义SimpleFeatureTypeBuilder typeBuilder = new SimpleFeatureTypeBuilder();//图层名typeBuilder.setName("myLayer");//空间坐标typeBuilder.setCRS(CRS.decode("EPSG:4490"));typeBuilder.add("the_geom", Point.class);//普通属性字段typeBuilder.add("id", String.class);typeBuilder.length(10).add("name", String.class);//字段长度typeBuilder.add("number",Integer.class);typeBuilder.add("double",Double.class);typeBuilder.add("time",Date.class);SimpleFeatureType featureType = typeBuilder.buildFeatureType();//创建feature定义到shapedataStore.createSchema(featureType);/** * 写数据到shape文件 *  * *///shape中feature定义Map params = new HashMap();params.put("url", file.toURI().toURL());DataStore dataStore2 = DataStoreFinder.getDataStore(params);String typeName = dataStore2.getTypeNames()[0];SimpleFeatureSource featureSource = dataStore2.getFeatureSource(typeName);SimpleFeatureType schema = featureSource.getSchema();//事务处理Transaction transaction = new DefaultTransaction("create");if (featureSource instanceof SimpleFeatureStore) {    SimpleFeatureStore featureStore = (SimpleFeatureStore) featureSource;    //创建一条数据 feature    List features = new ArrayList();    GeometryFactory geometryFactory = JTSFactoryFinder.getGeometryFactory();    SimpleFeatureBuilder featureBuilder = new SimpleFeatureBuilder(schema);    Point point = geometryFactory.createPoint(new Coordinate(106.69563874,29.563694210810283));    featureBuilder.set("the_geom",point);    featureBuilder.set("id","id1");    featureBuilder.set("name","name1");    featureBuilder.set("number1",100);    featureBuilder.set("number2",66.0);    featureBuilder.set("time",new Date());    SimpleFeature feature = featureBuilder.buildFeature(null);    features.add(feature);    SimpleFeatureCollection collection = new ListFeatureCollection(schema, features);    featureStore.setTransaction(transaction);    try {        //写入shape中        featureStore.addFeatures(collection);        transaction.commit();    } catch (Exception e) {        e.printStackTrace();        transaction.rollback();    } finally {        transaction.close();    }} else {    System.out.println("写入失败");}/** * 读取shape文件数据 *  * */DataStore dataStore3 = new ShapefileDataStore(file.toURI().toURL());String typeName = dataStore3.getTypeNames()[0];FeatureSource source =dataStore3.getFeatureSource(typeName);Filter filter = Filter.INCLUDE;FeatureCollection collection = source.getFeatures(filter);try (FeatureIterator features = collection.features()) {    //features必须关闭,否则会造成内存泄漏    while (features.hasNext()) {        SimpleFeature feature = features.next();        feature.getProperties().stream().peek(e-> System.out.println(e.getName().getLocalPart() + " = " + e.getValue()));    }}