GeoTools
  1. GeoTools
  2. GEOT-192

Create tutorial or howto that adapts Spearfish sample to PostGIS

    Details

    • Type: Wish Wish
    • Status: Closed Closed
    • Priority: Minor Minor
    • Resolution: Fixed
    • Affects Version/s: 2.0-b5
    • Fix Version/s: None
    • Component/s: docs
    • Labels:
      None

      Description

      A number of users recently have been asking how to adapt the spearfish sample to use a PostGIS data store. I'm hoping that I can provide a few hints here and some one will pick up this issue and and test out the suggestions, and then modify the wiki to use postgis instead of shapefile.

      I believe the first step would be to change the creation of the Shapefile DataStore from instantiating directly to using the DataStoreFinder class. Instead of:

      URL roadsURL = getResource("org/geotools/sampleData/roads.shp");
      ShapefileDataStore dsRoads = new ShapefileDataStore(roadsURL);

      It should be:

      URL roadsURL = getResource("org/geotools/sampleData/roads.shp");
      Map params = new HashMap().add("url", roadsURL);
      DataStore dsRoads = DataStoreFinder.getDataStore(params);

      Yes, this is an extra step, but it makes use of the interface for pluggable datastores, so we can easily use postgis. To use postgis you need to use the postgis params. The appropriate params to initiate a postgis DataStore are: dbtype (which must always be 'postgis'), host, port, database, user, passwd.
      host is the name of the computer the db is stored on, localhost if on the same computer.
      port is generally 5432 (postgis must be run with -i for gt2 to connect)
      database is the name of the db to connect to.
      user and passwd are pretty self explanatory - passwd is optional.

      So to instantiate a postgis datastore the code would look like:
      Map params = new HashMap();
      params.add("dbtype", "postgis");
      params.add("host", "localhost");
      params.add("port", "5432");
      params.add("user", "cholmes");
      DataStore pgDatastore = DataStoreFinder.getDataStore(params);

      This should give you the datastore. Then you need to select the FeatureSources. PostGIS DataStore represents the whole database, so it should contain all the feature sources, so you just need to say:

      FeatureSource fsRoads = pgDatastore.getFeatureSource("roads");
      FeatureSource fsStreams = pgDatastore.getFeatureSource("streams");
      ect.

      To ensure success with the spearfish sample the shapefiles should be converted to a tables in the same database using shp2pgsql. If someone makes the sql file it would be nice to post it for other users.

      The created featureSources should be added directly as layers, just as with shapefile, no need to extract points from them.

      I'm sure there will be more questions, as I haven't actually set this up myself. But ask them as comments on this issue, and I should be able to answer. And please document the results of this issue on the wiki.

      I leave this open for someone to help out on, just assign it to yourself and close then done - doing so would be a great way to help out the geotools project.

        Activity

        Hide
        Rueben Schulz added a comment -
        I have finished an answer to this problem at:
        http://docs.codehaus.org/display/GEOTOOLS/How+to+use+a+PostGISDataStore

        Can other people please test this when they have time. Also we need a page showing available params for the different DataStores. I will start one tomorrow.
        Show
        Rueben Schulz added a comment - I have finished an answer to this problem at: http://docs.codehaus.org/display/GEOTOOLS/How+to+use+a+PostGISDataStore Can other people please test this when they have time. Also we need a page showing available params for the different DataStores. I will start one tomorrow.
        Hide
        added a comment -
        Hi Chris,

        Thanx a lot, It was just a classpath problem, because i was including both of them in my classpath. Bcause I was including all the jar files I couldn't figure that out. But it did work finally.

        I could draw the map.

        Thank you,
        gayatri.
        Show
        added a comment - Hi Chris, Thanx a lot, It was just a classpath problem, because i was including both of them in my classpath. Bcause I was including all the jar files I couldn't figure that out. But it did work finally. I could draw the map. Thank you, gayatri.
        Hide
        added a comment -
        Hi,

        I did test the spearfish example too and it worked, I could see the map.

        When i installed geotools the jar files too be included were in jars and lib directories. While running my program I always used to set my classpath to all the jar files given, I could not realize tat I was including the old postgresql.jar file too there. I really had tough time, because the postgresql.jar file came along with geotools installation and it really created problems for me, could you modify that or write a comment in the example about this issue. I really had tough time figuring this out.

        Thanks,
        gayatri.
        Show
        added a comment - Hi, I did test the spearfish example too and it worked, I could see the map. When i installed geotools the jar files too be included were in jars and lib directories. While running my program I always used to set my classpath to all the jar files given, I could not realize tat I was including the old postgresql.jar file too there. I really had tough time, because the postgresql.jar file came along with geotools installation and it really created problems for me, could you modify that or write a comment in the example about this issue. I really had tough time figuring this out. Thanks, gayatri.
        Hide
        Chris Holmes added a comment -
        Yeah, the jar has been updated in geotools, the next release will have the right jar file.

        Also, if you have a bit could you check over the http://docs.codehaus.org/display/GEOTOOLS/How%2Bto%2Buse%2Ba%2BPostGISDataStore
        If you find it sufficient then we can close this issue. Actually it would probably be good if you added the note about your experience, to tell users to be sure not to include the jar in the lib/ directory.
        Show
        Chris Holmes added a comment - Yeah, the jar has been updated in geotools, the next release will have the right jar file. Also, if you have a bit could you check over the http://docs.codehaus.org/display/GEOTOOLS/How%2Bto%2Buse%2Ba%2BPostGISDataStore If you find it sufficient then we can close this issue. Actually it would probably be good if you added the note about your experience, to tell users to be sure not to include the jar in the lib/ directory.
        Hide
        added a comment -
        Hi Chris,

        I checked the document I think that is fine, those who use PostGIS should not have a problem of creating the database at all. All the steps though are provided. And after creating the feature source the next set of steps are same as of drawing a map from shapefile. So, I think that would be it. But I had to to install JAI to draw the map, but its not mentioned in the tutorial. I do not know if others have to install JAI, but I am running my prgorams in Linux, so my platform requires this. So, when I compile my programs I inlude the jars of JAI too.

        I would certainly like to include that note in the example. So, I think you modify that, because I do not know how to add a note, you could just mention in the tutorial that before running the program its better to make sure not to include the old postgresql.jar from the lib in classpath.

        I am going to work on geotools further, so I would be in constant touch with you. Anyways, thanks for your help.

        Thanks,
        gayatri.
        Show
        added a comment - Hi Chris, I checked the document I think that is fine, those who use PostGIS should not have a problem of creating the database at all. All the steps though are provided. And after creating the feature source the next set of steps are same as of drawing a map from shapefile. So, I think that would be it. But I had to to install JAI to draw the map, but its not mentioned in the tutorial. I do not know if others have to install JAI, but I am running my prgorams in Linux, so my platform requires this. So, when I compile my programs I inlude the jars of JAI too. I would certainly like to include that note in the example. So, I think you modify that, because I do not know how to add a note, you could just mention in the tutorial that before running the program its better to make sure not to include the old postgresql.jar from the lib in classpath. I am going to work on geotools further, so I would be in constant touch with you. Anyways, thanks for your help. Thanks, gayatri.

          People

          • Assignee:
            Rueben Schulz
            Reporter:
            Chris Holmes
          • Votes:
            0 Vote for this issue
            Watchers:
            1 Start watching this issue

            Dates

            • Created:
              Updated:
              Resolved: