Hi, I have followed your clues and i modified the CRSLab.java to store shp into 
my Postgis database. Unfortunately, it did not work. 
I have tried to create new DataStore using two different classes 
1. I use:
//DataStoreFactorySpi factory = new PostgisDataStoreFactory();
//DataStore newDataStore = factory.createNewDataStore(params);
and I get the following message from debugger:

java.lang.UnsupportedOperationException: Postgis cannot create a new Database
    at 
org.geotools.data.postgis.PostgisDataStoreFactory.createNewDataStore(PostgisDataStoreFactory.java:256)
    at geotools.examples.PostGISLab.exportToShapefile(PostGISLab.java:149)
    at geotools.examples.PostGISLab.access$100(PostGISLab.java:66)
    at 
geotools.examples.PostGISLab$ExportShapefileAction.action(PostGISLab.java:322)
    at org.geotools.swing.action.SafeAction.actionPerformed(SafeAction.java:47)
    at javax.swing.AbstractButton.fireActionPerformed(AbstractButton.java:2028)
    at 
javax.swing.AbstractButton$Handler.actionPerformed(AbstractButton.java:2351)
    at 
javax.swing.DefaultButtonModel.fireActionPerformed(DefaultButtonModel.java:387)
    at javax.swing.DefaultButtonModel.setPressed(DefaultButtonModel.java:242)
    at 
javax.swing.plaf.basic.BasicButtonListener.mouseReleased(BasicButtonListener.java:236)
    at java.awt.AWTEventMulticaster.mouseReleased(AWTEventMulticaster.java:272)
    at java.awt.Component.processMouseEvent(Component.java:6348)
    at javax.swing.JComponent.processMouseEvent(JComponent.java:3267)
    at java.awt.Component.processEvent(Component.java:6113)
    at java.awt.Container.processEvent(Container.java:2085)
    at java.awt.Component.dispatchEventImpl(Component.java:4714)
    at java.awt.Container.dispatchEventImpl(Container.java:2143)
    at java.awt.Component.dispatchEvent(Component.java:4544)
    at java.awt.LightweightDispatcher.retargetMouseEvent(Container.java:4618)
    at java.awt.LightweightDispatcher.processMouseEvent(Container.java:4282)
    at java.awt.LightweightDispatcher.dispatchEvent(Container.java:4212)
    at java.awt.Container.dispatchEventImpl(Container.java:2129)
    at java.awt.Window.dispatchEventImpl(Window.java:2478)
    at java.awt.Component.dispatchEvent(Component.java:4544)
    at java.awt.EventQueue.dispatchEvent(EventQueue.java:635)
    at 
java.awt.EventDispatchThread.pumpOneEventForFilters(EventDispatchThread.java:296)
    at 
java.awt.EventDispatchThread.pumpEventsForFilter(EventDispatchThread.java:211)
    at 
java.awt.EventDispatchThread.pumpEventsForHierarchy(EventDispatchThread.java:201)
    at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:196)
    at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:188)
    at java.awt.EventDispatchThread.run(EventDispatchThread.java:122)

2. or I use DataStore newDataStore = DataStoreFinder.getDataStore(params);
 taken form here:
http://docs.codehaus.org/display/GEOTDOC/PostGIS 
It seams that the table is created in postgis, but it is empty and when I try 
to access the data pgAdmin 3 stops to respond

if you have any constructive feedback let me know 

Thanks in advance 

the method responsible for this mess is bellow:

Cheers Kuba 

private void exportToShapefile() throws Exception {
        System.out.println("export1");
        SimpleFeatureType schema = featureSource.getSchema();
        CoordinateReferenceSystem dataCRS = 
schema.getCoordinateReferenceSystem();
        CoordinateReferenceSystem worldCRS = map.getCoordinateReferenceSystem();
        boolean lenient = true; // allow for some error due to different datums
        MathTransform transform = CRS.findMathTransform(dataCRS, worldCRS, 
lenient);
        // grab all features
        SimpleFeatureCollection featureCollection = featureSource.getFeatures();
        // And create a new Shapefile with a slight modified schema
        Map<String, Serializable> params = new HashMap<String, Serializable>();
        PostGISManager p = new PostGISManager("prop.properties");
        params.put(PostgisDataStoreFactory.DBTYPE.key, "postgis");
        params.put(PostgisDataStoreFactory.HOST.key, "localhost");
        params.put(PostgisDataStoreFactory.PORT.key, 5432);
        params.put(PostgisDataStoreFactory.SCHEMA.key, "public");
        params.put(PostgisDataStoreFactory.DATABASE.key, "riodejaneiro");
        params.put(PostgisDataStoreFactory.USER.key, "postgres");
        params.put(PostgisDataStoreFactory.PASSWD.key, "postgres");


        //DataStoreFactorySpi factory = new PostgisDataStoreFactory();
        //DataStore newDataStore = factory.createNewDataStore(params);

        DataStore newDataStore = DataStoreFinder.getDataStore(params);

        SimpleFeatureType featureType = SimpleFeatureTypeBuilder.retype(schema, 
worldCRS);
        newDataStore.createSchema(featureType);


        // carefully open an iterator and writer to process the results
        Transaction transaction = new DefaultTransaction("Reproject");
        FeatureWriter<SimpleFeatureType, SimpleFeature> writer = 
newDataStore.getFeatureWriterAppend(featureType.getTypeName(), transaction);
        SimpleFeatureIterator iterator = featureCollection.features();
        System.out.println("just before creating ");
        try {
            while (iterator.hasNext()) {
                // copy the contents of each feature and transform the geometry
                SimpleFeature feature = iterator.next();
                SimpleFeature copy = writer.next();
                copy.setAttributes(feature.getAttributes());

                Geometry geometry = (Geometry) feature.getDefaultGeometry();
                Geometry geometry2 = JTS.transform(geometry, transform);

                copy.setDefaultGeometry(geometry2);
                writer.write();
            }
            transaction.commit();
            JOptionPane.showMessageDialog(null, "Export to shapefile complete");
        } catch (Exception problem) {
            problem.printStackTrace();
            transaction.rollback();
            JOptionPane.showMessageDialog(null, "Export to shapefile failed");
        } finally {
            writer.close();
            iterator.close();
            transaction.close();
        }


    }
On Sep 3, 2010, at 3:42 PM, Jody Garnett wrote:

> You can use GeoTools to do this; copy a feature collection from your 
> shapefile into PostGIS; there is an example or two in the documentation of 
> read in a shapefile and write out a shapefile in a different projection; this 
> is similar except that you are writing to postgis.
> 
> Jody
> 
> On 03/09/2010, at 7:33 PM, Jakub Rojek wrote:
> 
>> hi, 
>> 
>> 
>> I am writing application that will manage my data resources. One of the 
>> element of this application should import shp files to the postgis database. 
>> I wonder if there already exists code that would do it or I need to do it 
>> from scratch.
>> 
>> 
>> Cheers 
>> 
>> 
>> Kuba 
>> 
>> 
>> 
>> ------------------------------------------------------------------------------
>> This SF.net Dev2Dev email is sponsored by:
>> 
>> Show off your parallel programming skills.
>> Enter the Intel(R) Threading Challenge 2010.
>> http://p.sf.net/sfu/intel-thread-sfd
>> _______________________________________________
>> Geotools-gt2-users mailing list
>> [email protected]
>> https://lists.sourceforge.net/lists/listinfo/geotools-gt2-users
> 

------------------------------------------------------------------------------
This SF.net Dev2Dev email is sponsored by:

Show off your parallel programming skills.
Enter the Intel(R) Threading Challenge 2010.
http://p.sf.net/sfu/intel-thread-sfd
_______________________________________________
Geotools-gt2-users mailing list
[email protected]
https://lists.sourceforge.net/lists/listinfo/geotools-gt2-users

Reply via email to