Setting projection on shapefile

3 messages Options
Embed this post
Permalink
Alex-29

Setting projection on shapefile

Reply Threaded More More options
Print post
Permalink
Hi,

I am trying to create a litle tool to set the projection on shapefiles that don't have their projection info set.

I know what the projection is and theirfore I don't need to reproject.
I've tried the samples available in the wiki with no success.

Basically I just want to update the existing shapefiles with the appropriate CRS. I don't really need to create new ones.

Is there a simple way to do this ?


The code below seems to simply copy my shapefiles to a different directory. Their projection info doesn't seem to have changed ...



                ShapefileDataStore dataStore =  new ShapefileDataStore(inputFile.toURI().toURL());
                String[] typeNames = dataStore.getTypeNames();
                String typeName = typeNames[0];

                DefaultQuery q = new DefaultQuery();
                q.setCoordinateSystem(originCrs);
               
                System.out.println("Reading content " + typeName);
                FeatureSource<SimpleFeatureType, SimpleFeature> featureSource = dataStore.getFeatureSource(typeName);
                SimpleFeatureType simpleFeatureType = featureSource.getSchema();
                System.out.println("Header: " + DataUtilities.spec( simpleFeatureType ));

               
                File resultFile = new File( destFolderPath + children[i]);
                FeatureCollection<SimpleFeatureType, SimpleFeature> collection = featureSource.getFeatures(q);
                DataStoreFactorySpi factory = new ShapefileDataStoreFactory();

                Map<String, Serializable> create = new HashMap<String,Serializable>();
                create.put("url", resultFile.toURI().toURL());
                create.put("create spatial index", Boolean.TRUE);
                DataStore newDataStore = factory.createNewDataStore(create);

                newDataStore.createSchema(collection.getSchema());
                Transaction transaction = new DefaultTransaction();
                FeatureStore<SimpleFeatureType, SimpleFeature> featureStore;
                featureStore = (FeatureStore<SimpleFeatureType, SimpleFeature>) newDataStore
                        .getFeatureSource(typeName);

               
                featureStore.setTransaction(transaction);
                try {
                    featureStore.addFeatures(collection);
                    transaction.commit();
                } catch (Exception problem) {
                    problem.printStackTrace();
                    transaction.rollback();
                }
                finally {
                    transaction.close();
                }




Many thanks for your help.

------------------------------------------------------------------------------
Come build with us! The BlackBerry® Developer Conference in SF, CA
is the only developer event you need to attend this year. Jumpstart your
developing skills, take BlackBerry mobile applications to market and stay
ahead of the curve. Join us from November 9-12, 2009. Register now!
http://p.sf.net/sfu/devconf
_______________________________________________
Geotools-gt2-users mailing list
[hidden email]
https://lists.sourceforge.net/lists/listinfo/geotools-gt2-users
mbedward

Re: Setting projection on shapefile

Reply Threaded More More options
Print post
Permalink
Hi Alex,

Here is one way to do it...

CoordinateReferenceSystem crs = ...
ShapefileDataStore store = ...
store.forceSchemaCRS( crs );
store.dispose();

Michael

------------------------------------------------------------------------------
Come build with us! The BlackBerry® Developer Conference in SF, CA
is the only developer event you need to attend this year. Jumpstart your
developing skills, take BlackBerry mobile applications to market and stay
ahead of the curve. Join us from November 9-12, 2009. Register now!
http://p.sf.net/sfu/devconf
_______________________________________________
Geotools-gt2-users mailing list
[hidden email]
https://lists.sourceforge.net/lists/listinfo/geotools-gt2-users
mbedward

Re: Setting projection on shapefile

Reply Threaded More More options
Print post
Permalink
2009/10/3 Alex wrote:

> Now for my second tool I would like to batch reproject them in a new folder.
>
> I tried the sample given here :
> http://docs.codehaus.org/display/GEOTOOLS/ShapeReprojector
> But I get a compilation error at
>
> writer.setFeatures(reprojectedSource.getFeatures().reader());
>
> It seems the reader() method does not exist anymore. Probably the
> documentation is for an older version.
> What can I do ?
>

Hi Alex,

There is a GeoTools example that could have been written just for you :-)

http://svn.osgeo.org/geotools/trunk/demo/example/src/main/java/org/geotools/demo/Shp2Shp.java

This will run with GeoTools version 2.6-M3

If you are using GeoTools version 2.5.x and want to stick with that
you can refer to this earlier version of the same class:

http://svn.osgeo.org/geotools/tags/2.5.7/demo/example/src/main/java/org/geotools/demo/Shp2Shp.java

Hope this helps.

Michael

------------------------------------------------------------------------------
Come build with us! The BlackBerry® Developer Conference in SF, CA
is the only developer event you need to attend this year. Jumpstart your
developing skills, take BlackBerry mobile applications to market and stay
ahead of the curve. Join us from November 9-12, 2009. Register now!
http://p.sf.net/sfu/devconf
_______________________________________________
Geotools-gt2-users mailing list
[hidden email]
https://lists.sourceforge.net/lists/listinfo/geotools-gt2-users