Fetching Data using a WKBGetter

A WKBGetter is the simplest way to extract data using WKB4J. The following class is available in the org.wkb4j.examples package. Basically a WKBGetter fetch the data in the submitted column from the submitted table, selected by the submitted whereQuery.

public class SampleGetter {
	protected static Logger Log = Logger.getLogger(SampleGetter.class);
	public static void main(String[] args) {
		try {
			/* Configure the logger so it will not complain and will be useful.*/
			BasicConfigurator.configure();

			/* Opens a connection to the PostGIS server.*/
			Class.forName("org.postgresql.Driver");
			String url = "jdbc:postgresql://localhost/wkb4j";
			Connection conn =
				DriverManager.getConnection(url, "postgres", "nothing");

			/* Use a Getter to fetch the geometries from the database.*/

			JTSGetter getter = new JTSGetter();
			List geomlist = getter.getData(conn, "road_lines", "the_geom", "");

          		/* Do something with the geometries.*/

		} catch (Exception e) {
			e.printStackTrace();
		}
	}
}

Fetching Data Manually

Sometimes the WKBGetter is not the best solution especially if you have specific needs. In this case, you will have to fetch data manually. Here is an example The SampleCode here does pretty much the same thing as the WKBGetter.

public class SampleCode {
    protected static Logger Log = Logger.getLogger(SampleCode.class);
    public static void main(String[] args) {
        try {
            /* Configure the logger so it will not complain and will be usefull.*/
            BasicConfigurator.configure();

            /* Opens a connection to the PostGIS server.*/
            Class.forName("org.postgresql.Driver");
            String url = "jdbc:postgresql://localhost/wkb4j";
            Connection conn =
                DriverManager.getConnection(url, "postgres", "nothing");

            /* Create the WKBReader.*/
            WKBReader reader = new WKBReader();

            /* For this demo we will be using the PostGISFactory.*/
            PostGISFactory factory = new PostGISFactory();

            /* Create the WKBParser.*/
            WKBParser parser = new WKBParser(factory);

            /* You can complement this query with your own code.*/
            reader.readDataWithPartialQuery(
                conn,
                "the_geom",
                "from points",
                parser);

            /* In the PostGISFactory, completed Geometries are stored internally
             * and can be returned through this method.*/
            ArrayList geomlist = factory.getGeometries();

          /* Do something with the geometries.*/

        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}