2014-11-14 41 views
1

我有一个需要放置在形状文件中的JTS几何对象数组。我还有一些其他属性需要在DBase文件中进行。我需要为空间对象编制索引,如果需要的话创建投影文件。有没有办法使用JTS/GeoTools来做到这一点。我尝试过ShapeFileWriter,但这似乎不够(例如没有dbf支持)。需要帮助将JTS几何对象数组转换为形状文件

public Shape(String shpFileName, String shxFileName) throws FileNotFoundException { 
    RandomAccessFile shpFile  = new RandomAccessFile(shpFileName, "rw"); 
    this.shpChannel = shpFile.getChannel(); 
    RandomAccessFile shxFile = new RandomAccessFile(shxFileName, "rw"); 
    this.shxChannel = shxFile.getChannel(); 
} 

public void createShapeFile(GeometryCollection geometries, ShapeType shapeType) throws IOException { 
    ShapefileWriter writer = new ShapefileWriter(this.shpChannel, this.shxChannel); 
    writer.write(geometries, shapeType); 
    writer.close(); 


} 
+0

Shapefilewriter会做所有这一切 - 可能是你所能向我们展示一些代码 – 2014-11-15 17:15:22

+0

我已经在问题中添加了部分代码以供参考。 @iant – 2014-11-16 20:22:21

回答

0

你不能写出一组几何图形并得到一个DBF文件(因为没有放置属性)。您需要创建一个FeatureCollection,然后将其传递给ShapeFileDatastore。

你会需要这样的东西:

public boolean writeFeatures(
     FeatureCollection<SimpleFeatureType, SimpleFeature> features) { 

    if (shpDataStore == null) { 
     throw new IllegalStateException(
       "Datastore can not be null when writing"); 
    } 
    SimpleFeatureType schema = features.getSchema(); 
    GeometryDescriptor geom = schema 
      .getGeometryDescriptor(); 

    try { 

     /* 
     * Write the features to the shapefile 
     */ 
     Transaction transaction = new DefaultTransaction(
       "create"); 

     String typeName = shpDataStore.getTypeNames()[0]; 
     SimpleFeatureSource featureSource = shpDataStore 
       .getFeatureSource(typeName); 

     /* 
     * The Shapefile format has a couple limitations: - "the_geom" is always 
     * first, and used for the geometry attribute name - "the_geom" must be of 
     * type Point, MultiPoint, MuiltiLineString, MultiPolygon - Attribute 
     * names are limited in length - Not all data types are supported (example 
     * Timestamp represented as Date) 
     * 
     * Because of this we have to rename the geometry element and then rebuild 
     * the features to make sure that it is the first attribute. 
     */ 

     List<AttributeDescriptor> attributes = schema 
       .getAttributeDescriptors(); 
     GeometryType geomType = null; 
     List<AttributeDescriptor> attribs = new ArrayList<AttributeDescriptor>(); 
     for (AttributeDescriptor attrib : attributes) { 
      AttributeType type = attrib.getType(); 
      if (type instanceof GeometryType) { 
       geomType = (GeometryType) type; 

      } else { 
       attribs.add(attrib); 
      } 
     } 

     GeometryTypeImpl gt = new GeometryTypeImpl(
       new NameImpl("the_geom"), geomType.getBinding(), 
       geomType.getCoordinateReferenceSystem(), 
       geomType.isIdentified(), geomType.isAbstract(), 
       geomType.getRestrictions(), geomType.getSuper(), 
       geomType.getDescription()); 

     GeometryDescriptor geomDesc = new GeometryDescriptorImpl(
       gt, new NameImpl("the_geom"), 
       geom.getMinOccurs(), geom.getMaxOccurs(), 
       geom.isNillable(), geom.getDefaultValue()); 

     attribs.add(0, geomDesc); 

     SimpleFeatureType shpType = new SimpleFeatureTypeImpl(
       schema.getName(), attribs, geomDesc, 
       schema.isAbstract(), schema.getRestrictions(), 
       schema.getSuper(), schema.getDescription()); 


     shpDataStore.createSchema(shpType); 

     if (featureSource instanceof SimpleFeatureStore) { 
      SimpleFeatureStore featureStore = (SimpleFeatureStore) featureSource; 

      List<SimpleFeature> feats = new ArrayList<SimpleFeature>(); 

      FeatureIterator<SimpleFeature> features2 = features 
        .features(); 
      while (features2.hasNext()) { 
       SimpleFeature f = features2.next(); 
       SimpleFeature reType = SimpleFeatureBuilder 
         .build(shpType, f.getAttributes(), ""); 

       feats.add(reType); 
      } 
      features2.close(); 
      SimpleFeatureCollection collection = new ListFeatureCollection(
        shpType, feats); 

      featureStore.setTransaction(transaction); 
      try { 
       List<FeatureId> ids = featureStore 
         .addFeatures(collection); 
       transaction.commit(); 
      } catch (Exception problem) { 
       problem.printStackTrace(); 
       transaction.rollback(); 
      } finally { 
       transaction.close(); 
      } 
      shpDataStore.dispose(); 
      return true; 
     } else { 
      shpDataStore.dispose(); 
      System.err.println("ShapefileStore not writable"); 
      return false; 
     } 
    } catch (IOException e) { 
     e.printStackTrace(); 
    } 
    return false; 
} 

有一个在https://github.com/ianturton/geotools-cookbook/tree/master/modules/output/src/main/java/org/ianturton/cookbook/output一个完整的工作示例(包括POM文件理清依赖)