Hi Rainer,
Here is a code example which shows how to write from a shapefile to a
postgis table.
I inserted your PostGIS parameters in that code.
Hope it can help you.
Cheers,
Eric
/**
* Load a given shapefile into the default PostGIS database using the
postgisTableName
* parameter as new name for the table creation. The management of the
projection system
* is done: if there is a valid SRID (in regard with the EPSG
references) in the PRJ file,
* this SRID is taken into account. Otherwise, a WGS84 (EPSG:4326)
projection system
* is used.
*
* @param shapefileURL URL of the shapefile
* @param postgisTableName name of the PostGIS table to create
* @return true if loaded, false otherwise
*
* @throws FactoryException
*/
public boolean loadShapefileIntoDefaultPostGI
S(URL shapefileURL, String postgisTableName) throws FactoryException {
try {
// shapefile loader
Map<Object,Serializable> shapeParams = new
HashMap<Object,Serializable>();
shapeParams.put("url", shapefileURL);
DataStore shapeDataStore =
DataStoreFinder.getDataStore(shapeParams);
// feature type
String typeName = shapeDataStore.getTypeNames()[0];
FeatureSource<SimpleFeatureType,SimpleFeature> featSource =
shapeDataStore.getFeatureSource(typeName);
FeatureCollection<SimpleFeatureType,SimpleFeature>
featSrcCollection = featSource.getFeatures();
SimpleFeatureType ft = shapeDataStore.getSchema(typeName);
// feature type copy to set the new name
SimpleFeatureTypeBuilder builder = new
SimpleFeatureTypeBuilder();
builder.setName(postgisTableName);
builder.setAttributes(ft.getAttributeDescriptors());
builder.setCRS(ft.getCoordinateReferenceSystem());
SimpleFeatureType newSchema = builder.buildFeatureType();
// management of the projection system
CoordinateReferenceSystem crs =
ft.getCoordinateReferenceSystem();
// test of the CRS based on the .prj file
Integer crsCode = CRS.lookupEpsgCode(crs, true);
Set<ReferenceIdentifier> refIds =
ft.getCoordinateReferenceSystem().getIdentifiers();
if ( ( (refIds == null) || (refIds.isEmpty() ) ) && (crsCode ==
null) ) {
CoordinateReferenceSystem crsEpsg = CRS.decode("EPSG:4326");
newSchema =
SimpleFeatureTypeBuilder.retype(newSchema,crsEpsg);
}
Map postGISparams = new HashMap();
params.put("dbtype", "postgis");
params.put("host", "localhost");
params.put("port", new Integer(5432));
params.put("database", "my_db");
params.put("user", "postgres");
params.put("passwd", "postgres");
// storage in PostGIS
DataStore dataStore =
DataStoreFinder.getDataStore(postGISparams);
if (dataStore == null) {
return false;
}
dataStore.createSchema(newSchema);
FeatureStore<SimpleFeatureType,SimpleFeature> featStore =
(FeatureStore<SimpleFeatureType,SimpleFeature>)dataStore.getFeatureSource(postgisTableName);
featStore.addFeatures(featSrcCollection);
} catch (IOException e) {
e.printStackTrace();
return false;
}
return true;
On 27 May 2011 16:59, Rainer Suddendorf <[email protected]> wrote:
> Hi,
>
> I am a bit helpless, how to write a feature to a postgis datastore.
> I managed to read, but not to write.
> For simplicity I show my example code for deleting a feature from the
> database.
> public static void main(String[] args) throws Exception {
> Map params = new HashMap();
> params.put("dbtype", "postgis");
> params.put("host", "localhost");
> params.put("port", new Integer(5432));
> params.put("database", "my_db");
> params.put("user", "postgres");
> params.put("passwd", "postgres");
>
> PostgisDataStore dataStore = (PostgisDataStore) DataStoreFinder
> .getDataStore(params);
>
> Transaction t = new DefaultTransaction("handle");
>
> FeatureSource<SimpleFeatureType, SimpleFeature> fs = dataStore
> .getFeatureSource("points");
>
> Filter filter = CQL.toFilter("id=1");
> FeatureCollection fc = fs.getFeatures(filter);
> FeatureIterator fi = fc.features();
> while (fi.hasNext()) {
> SimpleFeature f = (SimpleFeature) fi.next();
> System.out.println(f);
> fs.getFeatures().remove(f);
> }
> t.commit();
> }
>
> Thanks for Your help
>
> Rainer
>
>
>
> --
> SDDB Consulting GmbH
> Wilhelm-Busch Str. 3
> 31832 Springe
>
> Phone +49 5045 962773
> Mobil +49 178 716 20 11
> Mail [email protected]
>
>
>
>
> ------------------------------------------------------------------------------
> vRanger cuts backup time in half-while increasing security.
> With the market-leading solution for virtual backup and recovery,
> you get blazing-fast, flexible, and affordable data protection.
> Download your free trial now.
> http://p.sf.net/sfu/quest-d2dcopy1
> _______________________________________________
> Geotools-gt2-users mailing list
> [email protected]
> https://lists.sourceforge.net/lists/listinfo/geotools-gt2-users
>
------------------------------------------------------------------------------
vRanger cuts backup time in half-while increasing security.
With the market-leading solution for virtual backup and recovery,
you get blazing-fast, flexible, and affordable data protection.
Download your free trial now.
http://p.sf.net/sfu/quest-d2dcopy1
_______________________________________________
Geotools-gt2-users mailing list
[email protected]
https://lists.sourceforge.net/lists/listinfo/geotools-gt2-users