[Geoserver-devel] Modify geometry of feature?

Hi list,
I have to develop a module that modify geometry of polygon. Is there anyone worked with same function in geotools?
Any suggestion is good for me.
Thanks.

Hi, You can find a wealth of information in the user guide, specifically the feature tutorial: The following section is about writing features: To modify a feature, instead of SimpleFeatureStore.addFeatures() you should call the SimpleFeatureStore.modifyFeatures() method, documented at: Note that you need to reference the feature you are going to modify with the “filter” parameter. To do this, your best bet is to create a filter on the feature ID you get when calling SimpleFeature.getID() as follows: // get feature SimpleFeature myFeature = …; String fid = myFeature.getID(); // get the geometry com.vividsolutions.jts.geom.Geometry g = (com.vividsolutions.jts.geom.Geometry)myFeature.getDefaultGeometry(); // modify it g = g.buffer(10.0); // save it SimpleFeatureStore store = …; Transaction transaction = new DefaultTransaction(“edit”); store.setTransaction(transaction); // reference the feature to modify by FID FilterFactory2 ff = CommonFactoryFinder.getFilterFactory2(); Filter filter = ff.id(new FeatureIdImpl(fid)); store.modifyFeatures( store.getSchema().getGeometryDescriptor().getName(), g, filter); transaction.commit(); Matthijs

···

On 2012-07-23 11:31, Truong Pham Minh wrote:

Hi list,
I have to develop a module that modify geometry of polygon. Is there anyone worked with same function in geotools?
Any suggestion is good for me.
Thanks.

http://docs.geotools.org/latest/userguide/tutorial/feature/csv2shp.html

http://docs.geotools.org/latest/userguide/tutorial/feature/csv2shp.html#write-the-feature-data-to-the-shapefile

http://docs.geotools.org/latest/javadocs/index.html?org/geotools/data/simple/SimpleFeatureStore.html

Million thanks Matthijs Laan for your nice answer. It very helpful for me.

— Ngày Thứ 2, 23/07/12, Matthijs Laan <matthijslaan@anonymised.com> đã viết:

Từ: Matthijs Laan <matthijslaan@anonymised.com>
Chủ đề: Re: [Geoserver-devel] Modify geometry of feature?
Đến: geoserver-devel@lists.sourceforge.net
Ngày: Thứ Hai, 23 tháng 7, 2012, 6:39

On 2012-07-23 11:31, Truong Pham Minh wrote:

Hi list,
I have to develop a module that modify geometry of polygon. Is there anyone worked with same function in geotools?
Any suggestion is good for me.
Thanks.

Hi,

You can find a wealth of information in the user guide, specifically the feature tutorial:

http://docs.geotools.org/latest/userguide/tutorial/feature/csv2shp.html

The following section is about writing features:
http://docs.geotools.org/latest/userguide/tutorial/feature/csv2shp.html#write-the-feature-data-to-the-shapefile

To modify a feature, instead of SimpleFeatureStore.addFeatures() you should call the SimpleFeatureStore.modifyFeatures() method, documented at:
http://docs.geotools.org/latest/javadocs/index.html?org/geotools/data/simple/SimpleFeatureStore.html

Note that you need to reference the feature you are going to modify with the “filter” parameter. To do this, your best bet is to create a filter on the feature ID you get when calling SimpleFeature.getID() as follows:

// get feature
SimpleFeature myFeature = …;
String fid = myFeature.getID();

// get the geometry
com.vividsolutions.jts.geom.Geometry g = (com.vividsolutions.jts.geom.Geometry)myFeature.getDefaultGeometry();

// modify it
g = g.buffer(10.0);

// save it
SimpleFeatureStore store = …;
Transaction transaction = new DefaultTransaction(“edit”);
store.setTransaction(transaction);

// reference the feature to modify by FID
FilterFactory2 ff = CommonFactoryFinder.getFilterFactory2();
Filter filter = ff.id(new FeatureIdImpl(fid));

store.modifyFeatures( store.getSchema().getGeometryDescriptor().getName(), g, filter);
transaction.commit();

Matthijs

-----Thư đính kèm sau đây-----


Live Security Virtual Conference
Exclusive live event will cover all the ways today’s security and
threat landscape has changed and how IT managers can respond. Discussions
will include endpoint security, mobile security and the latest in malware
threats. http://www.accelacomm.com/jaw/sfrnl04242012/114/50122263/

-----Thư đính kèm sau đây-----


Geoserver-devel mailing list
Geoserver-devel@lists.sourceforge.net
https://lists.sourceforge.net/lists/listinfo/geoserver-devel