From: Chris H. <ch...@op...> - 2005-12-30 15:48:58
|
Quoting Marc Florisson <mar...@dr...>: > Hello, > > I'd like load a set of geometric data in a Postgis/Postgres database. > I have to do it in a single transaction. > But I don't know how to do it with FeatureWriter api. You actually shouldn't be using the FeatureWriter api. It's too low level, and indeed we've had thoughts of getting rid of it. The FeatureStore API provides all the equivalent functionality, plus a good bit more, and generally works better (in some implementations it uses the low level featureWriter api, but sometimes not). You first want to create a DefaultTransaction: transaction = new DefaultTransaction(); And you will use this against all the FeatureStores that you want to interact with. This can even do transactions across different DataStores. Any FeatureStore you use you say setTransaction. Then you call transaction.commit() when you're done. Though actually, looking at your code, you can do all that you want with a single addFeatures(reader). To get a reader you can use DataUtilities.reader(Feature[]). You could also make a MemoryDataStore and add it there, or perhaps construct a FeatureCollection and get a reader from there as well. If you just use addFeatures I believe most all DataStores should handle it in a single commit. You may use the transaction.commit() to be sure. If you want some sample code on working with this stuff, see: https://svn.codehaus.org/geoserver/trunk/geoserver/src/org/vfny/geoserver/wfs/responses/TransactionResponse.java Though it's currently the geoserver class most in need of refactoring, so it may be a bit hard to figure out. Oh, and there's a Snippet for MySQL here: http://geotools.org/display/GEOTOOLS/How+to+add+features+data+into+MySQL If you figure this out, it could be nice to put a more generic 'how to add features' code snippet. best regards, Chris > > Could anybody give me a solution ? > > PS: Here is an exemple: > I try to create 15 data (with a Point geometry) in a single > transaction. > > Map params = new HashMap(); > params.put("dbtype", "postgis"); //must be postgis > params.put("host", "localhost"); //the name or ip > address > of the machine running PostGIS > params.put("port", new Integer(5432)); //the port that PostGIS > is > running on (generally 5432) > params.put("database", "paris"); //the name of the > database > to connect to. > params.put("user", "postgres"); //the user to connect > with > params.put("passwd", "postgres"); //the password of the > user. > > try > { > // getting the FeatureSource > DataStore pgDatastore = DataStoreFinder.getDataStore(params); > FeatureSource fsBC = pgDatastore.getFeatureSource("lieux"); > > // getting the transaction > Transaction aTransaction = ((org.geotools.data.FeatureStore) > fsBC).getTransaction(); > > // creating the array of data to load together > int MAX = 15; > Feature[] aFeatureArray = new Feature[ MAX]; > FeatureWriter[] aFeatureWriterArray = new FeatureWriter[ > MAX]; > > for ( int i = 0; i < MAX; i++) > { > // feature : attributes creation > WKTReader wktReader = new WKTReader(); > Point geometry = (Point) wktReader. > read("POINT (608444.1 2425000.1)"); > > aFeatureWriterArray[ i] = > pgDatastore.getFeatureWriterAppend( > "lieux", > aTransaction); > aFeatureArray[ i] = aFeatureWriterArray[ i].next(); > aFeatureArray[ i].setAttribute( "the_geom", geometry); > aFeatureArray[ i].setAttribute( "type_gs", "appartement > "+i); > aFeatureArray[ i].setAttribute( "adresse", "25 rue Girard > de > Vasson"); > } > > aTransaction.commit(); // this line is useless > // How to commit all changes in a single transaction ? > > > -- > > Marc FLORISSON > mar...@dr... > www.dryade.net <http://www.dryade.net> > > > > ------------------------------------------------------- > This SF.net email is sponsored by: Splunk Inc. Do you grep through > log files > for problems? Stop! Download the new AJAX search engine that makes > searching your log files as easy as surfing the web. DOWNLOAD > SPLUNK! > http://ads.osdn.com/?ad_id=7637&alloc_id=16865&op=click > _______________________________________________ > Geotools-gt2-users mailing list > Geo...@li... > https://lists.sourceforge.net/lists/listinfo/geotools-gt2-users > ---------------------------------------------------------- This mail sent through IMP: https://webmail.limegroup.com/ |