|
|
|
svn_geotools
|
Author: jdeolive
Date: 2009-07-08 00:03:13 -0400 (Wed, 08 Jul 2009) New Revision: 33522 Added: trunk/demo/jdbc/ trunk/demo/jdbc/pom.xml trunk/demo/jdbc/src/ trunk/demo/jdbc/src/main/ trunk/demo/jdbc/src/main/java/ trunk/demo/jdbc/src/main/java/org/ trunk/demo/jdbc/src/main/java/org/geotools/ trunk/demo/jdbc/src/main/java/org/geotools/demo/ trunk/demo/jdbc/src/main/java/org/geotools/demo/jdbc/ trunk/demo/jdbc/src/main/java/org/geotools/demo/jdbc/JDBCDemo.java Modified: trunk/demo/pom.xml Log: added jdbc demo module Added: trunk/demo/jdbc/pom.xml =================================================================== --- trunk/demo/jdbc/pom.xml (rev 0) +++ trunk/demo/jdbc/pom.xml 2009-07-08 04:03:13 UTC (rev 33522) @@ -0,0 +1,64 @@ +<?xml version="1.0" encoding="UTF-8"?> +<!-- ======================================================================= + Maven Project Configuration File + + The Geotools Project + http://www.geotools.org/ + + Version: $Id$ + ======================================================================= --> + <project xmlns="http://maven.apache.org/POM/4.0.0" + xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" + xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 + http://maven.apache.org/maven-v4_0_0.xsd"> + <modelVersion>4.0.0</modelVersion> + + <parent> + <groupId>org.geotools.demo</groupId> + <artifactId>demo</artifactId> + <version>2.6-SNAPSHOT</version> + </parent> + + + <!-- =========================================================== --> + <!-- Module Description --> + <!-- =========================================================== --> + <groupId>org.geotools.demo</groupId> + <artifactId>gt-demo-jdbc</artifactId> + <packaging>jar</packaging> + <name>JDBC DataStore Example</name> + + <!-- =========================================================== --> + <!-- Dependency Management --> + <!-- =========================================================== --> + <dependencies> + <dependency> + <groupId>org.geotools.jdbc</groupId> + <artifactId>gt-jdbc-h2</artifactId> + <version>${project.version}</version> + </dependency> + <dependency> + <groupId>org.geotools.jdbc</groupId> + <artifactId>gt-jdbc-postgis</artifactId> + <version>${project.version}</version> + </dependency> + <dependency> + <groupId>org.geotools</groupId> + <artifactId>gt-epsg-hsql</artifactId> + <version>${project.version}</version> + </dependency> + </dependencies> + + <build> + <plugins> + <plugin> + <groupId>org.codehaus.mojo</groupId> + <artifactId>exec-maven-plugin</artifactId> + <configuration> + <mainClass>org.geotools.demo.jdbc.JDBCDemo</mainClass> + </configuration> + </plugin> + </plugins> + </build> + +</project> Property changes on: trunk/demo/jdbc/pom.xml ___________________________________________________________________ Added: svn:mime-type + text/xml Added: svn:keywords + Id URL Added: svn:eol-style + native Added: trunk/demo/jdbc/src/main/java/org/geotools/demo/jdbc/JDBCDemo.java =================================================================== --- trunk/demo/jdbc/src/main/java/org/geotools/demo/jdbc/JDBCDemo.java (rev 0) +++ trunk/demo/jdbc/src/main/java/org/geotools/demo/jdbc/JDBCDemo.java 2009-07-08 04:03:13 UTC (rev 33522) @@ -0,0 +1,138 @@ +package org.geotools.demo.jdbc; + +import java.util.HashMap; + +import org.geotools.data.DataStore; +import org.geotools.data.DataStoreFinder; +import org.geotools.data.DefaultQuery; +import org.geotools.data.FeatureSource; +import org.geotools.data.FeatureWriter; +import org.geotools.data.Query; +import org.geotools.data.Transaction; +import org.geotools.factory.CommonFactoryFinder; +import org.geotools.feature.FeatureCollection; +import org.geotools.feature.FeatureIterator; +import org.geotools.feature.simple.SimpleFeatureTypeBuilder; +import org.h2.tools.DeleteDbFiles; +import org.opengis.feature.simple.SimpleFeature; +import org.opengis.feature.simple.SimpleFeatureType; +import org.opengis.filter.FilterFactory; +import org.opengis.filter.spatial.BBOX; + +import com.vividsolutions.jts.geom.Coordinate; +import com.vividsolutions.jts.geom.GeometryFactory; +import com.vividsolutions.jts.geom.Point; + +/** + * Demo application illustrating the use of JDBC datastores. + * <p> + * This demo runs with an embedded H2 database, but could easily be + * adapted to PostGIS,Oracle,etc... + * </p> + * @author Justin Deoliveira + * + */ +public class JDBCDemo { + + public static void main(String[] args) throws Exception { + //clean up any old files + DeleteDbFiles.execute(System.getProperty("user.dir"),"acme",true); + + //get a new datastore + HashMap params = new HashMap(); + params.put( "dbtype", "h2"); + params.put( "database", "acme"); + + //To use postgis: + // 1. Uncomment the following, and comment out the above + // 2. Delete the acme table if it does exist + // 3. Change the parameters as necessary +// HashMap params = new HashMap(); +// params.put( "dbtype", "postgis"); +// params.put( "host", "localhost"); +// params.put( "port", "5432"); +// params.put( "database", "acme"); +// params.put( "user", "geotools"); +// params.put( "password", ""); + + DataStore ds = (DataStore) DataStoreFinder.getDataStore( params ); + + //create a new schema + SimpleFeatureTypeBuilder tb = new SimpleFeatureTypeBuilder(); + tb.setNamespaceURI( "http://acme.com"); + tb.setName( "widgets"); + tb.srs("EPSG:4326"); + tb.add( "geom", Point.class ); + tb.add( "name", String.class ); + tb.add( "type", Integer.class ); + tb.add( "price", Double.class ); + SimpleFeatureType type = tb.buildFeatureType(); + ds.createSchema( type ); + + //write some features + GeometryFactory gf = new GeometryFactory(); + FeatureWriter fw = ds.getFeatureWriter( "widgets", Transaction.AUTO_COMMIT ); + fw.hasNext(); + + SimpleFeature f = (SimpleFeature) fw.next(); + f.setAttribute( "geom", gf.createPoint(new Coordinate(1,1)) ); + f.setAttribute( "name", "foo" ); + f.setAttribute( "type", 1 ); + f.setAttribute( "price", 1.10 ); + fw.write(); + + fw.hasNext(); + f = (SimpleFeature) fw.next(); + + f.setAttribute( "geom", gf.createPoint(new Coordinate(10,10)) ); + f.setAttribute( "name", "bar" ); + f.setAttribute( "type", 10 ); + f.setAttribute( "price", 10.10 ); + fw.write(); + + fw.hasNext(); + f = (SimpleFeature) fw.next(); + f.setAttribute( "geom", gf.createPoint(new Coordinate(20,20)) ); + f.setAttribute( "name", "foobar" ); + f.setAttribute( "type", 20 ); + f.setAttribute( "price", 20.20 ); + fw.write(); + + fw.close(); + + //grab all features + FeatureSource fs = ds.getFeatureSource( "widgets"); + System.out.println( "Number of features = " + fs.getCount(Query.ALL) ); + + FeatureCollection features = fs.getFeatures(); + FeatureIterator fi = features.features(); + try { + while( fi.hasNext() ) { + f = (SimpleFeature) fi.next(); + System.out.println( f.getID() ); + } + } + finally { + features.close( fi ); + } + + //query some features + FilterFactory ff = CommonFactoryFinder.getFilterFactory( null ); + BBOX bbox = ff.bbox( "geom", 5, 5, 15, 15, "EPSG:4326"); + + DefaultQuery q = new DefaultQuery("widgets", bbox ); + features = ds.getFeatureSource("widgets").getFeatures( q ); + + System.out.println("Number of features = " + features.size() ); + fi = features.features(); + try { + while( fi.hasNext() ) { + f = (SimpleFeature) fi.next(); + System.out.println( f.getID() ); + } + } + finally { + features.close( fi ); + } + } +} Property changes on: trunk/demo/jdbc/src/main/java/org/geotools/demo/jdbc/JDBCDemo.java ___________________________________________________________________ Added: svn:mime-type + text/plain Added: svn:keywords + Id URL Added: svn:eol-style + native Modified: trunk/demo/pom.xml =================================================================== --- trunk/demo/pom.xml 2009-07-08 02:23:43 UTC (rev 33521) +++ trunk/demo/pom.xml 2009-07-08 04:03:13 UTC (rev 33522) @@ -104,6 +104,7 @@ <module>example</module> <module>svgsupport</module> <module>gmlparsing</module> + <module>jdbc</module> <!--module>xml-po</module--> </modules> ------------------------------------------------------------------------------ Enter the BlackBerry Developer Challenge This is your chance to win up to $100,000 in prizes! For a limited time, vendors submitting new applications to BlackBerry App World(TM) will have the opportunity to enter the BlackBerry Developer Challenge. See full prize details at: http://p.sf.net/sfu/Challenge _______________________________________________ GeoTools-commits mailing list [hidden email] https://lists.sourceforge.net/lists/listinfo/geotools-commits |
||||||||||||||||
| Free Embeddable Forum Powered by Nabble | Help |