I can already get the vertex coordinates of the polygon, but I want to modify the coordinates and write it back to the source file. This is part of the code I read the coordinates.

      while (iter.hasNext()) {
          SimpleFeature sf = iter.next();
          Collection<Property> property =((ComplexAttribute)sf).getProperties();
          Iterator<Property> iterP = property.iterator();
          while (iterP.hasNext()) {
              Property pro = iterP.next();
              if (pro.getValue() instanceof MultiPolygon) {
                  gisObjectList.add((T) new GisMultiPolygon ((MultiPolygon)pro.getValue(), (SimpleFeature) sf));
                   Geometry gc = (MultiPolygon) pro.getValue();
                  Coordinate[] points = gc.getCoordinates();

I created a new shapefile from the original shapefile, but there was a runtime error. error is org.geotools.data.directory.DirectoryDataStore cannot be cast to org.geotools.data.shapefile.ShapefileDataStore.

public static void transShape(File shpFile, File shpFile2) {
    try {
        ShapefileDataStore shapeDS = (ShapefileDataStore) new ShapefileDataStoreFactory().createDataStore(shpFile.toURI().toURL());
        Map<String, Serializable> params = new HashMap<String, Serializable>();
        FileDataStoreFactorySpi factory = new ShapefileDataStoreFactory();
        params.put(ShapefileDataStoreFactory.URLP.key, shpFile2.toURI().toURL());
        ShapefileDataStore ds = (ShapefileDataStore) factory.createNewDataStore(params);
        // 设置属性
        SimpleFeatureSource fs = shapeDS.getFeatureSource(shapeDS.getTypeNames()[0]);
        ds.createSchema(SimpleFeatureTypeBuilder.retype(fs.getSchema(), DefaultGeographicCRS.WGS84));
        FeatureWriter<SimpleFeatureType, SimpleFeature> writer = ds.getFeatureWriter(ds.getTypeNames()[0], Transaction.AUTO_COMMIT);
        SimpleFeatureIterator it = fs.getFeatures().features();
        try {
            while (it.hasNext()) {
                SimpleFeature f = it.next();
                SimpleFeature fNew = writer.next();
        } finally {
    } catch (Exception e) { e.printStackTrace();    }

You'll need a GeometryFactory:

GeometryFactory gf = new GeometryFactory();

And then you need to break each MultiPolygon up into it's Polygons and then for each Polygon deal with the exterior boundary and any holes in turn and then convert those back to a list of Polygons and finally build a new MultiPolygon.

               MultiPolygon gc = (MultiPolygon) pro.getValue();
               ArrayList<Polygon> polygons = new ArrayList<>();
               for(int i=0;i<gc.getNumGeometries();i++) {
                 Polygon p=(Polygon) gc.getGeometryN(i); 
                 Coordinate[] points = p.getExteriorRing().getCoordinates();
                 for (Coordinate c: points) {
                   // do whatever you want to the coords here
                   c.x += 10;
                   c.y += 10;
                 LinearRing shell = gf.createLinearRing(points);
                 LinearRing[] holes = new LinearRing[p.getNumInteriorRing()];
                 if(p.getNumInteriorRing()>0) {
                   for(int k=0;k<p.getNumInteriorRing();k++) {
                     Coordinate[] ring = p.getInteriorRingN(k).getCoordinates();
                     for (Coordinate c: points) {
                       // do whatever you want to the coords here
                       c.x += 10;
                       c.y += 10;
                     holes[k] = gf.createLinearRing(ring);
                 Polygon poly = gf.createPolygon(shell,holes);
              MultiPolygon nMP = gf.createMultiPolygon(polygons.toArray(new Polygon[] {}));
  • First of all, thank you for your help. But I want to make a coordinate replacement on the original file, not a new one – chicken Mar 20 at 1:21
  • then set the new multipolygon as the features default geom, but you will still need to write the features to disk – Ian Turton Mar 20 at 8:09
  • I have to create a new one and cannot modify it on the original file. – chicken Mar 20 at 9:29
  • A GeoTools FeatureCollection is an in memory representation of the file's contents, you can write it back over the original file if you are brave. Maybe you need to ask a new question that explains your broader problem – Ian Turton Mar 20 at 9:31
  • How can I write back the original file? – chicken Mar 20 at 9:32

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.