How do I programmatically add a layer from a PostGIS datastore?

classic Classic list List threaded Threaded
7 messages Options
Reply | Threaded
Open this post in threaded view
|

How do I programmatically add a layer from a PostGIS datastore?

Mark Mathis
All,

I am currently trying to develop a uDig custom application.  I have a
set of existing plugins that create or modify data in a PostGIS database
(behind the scenes using straight jdbc operations).  I can see the
results of my computations by manually refreshing the catalog (assuming
I have already connected to the postgres datastore) and using other
built in user interface functionality to add/remove layers and redraw
the map.

What I would like to do is, given the appropriate connection parameters
(host, database, table, and optional filter), programmatically add a new
layer to the map.  I have done this (more or less) successfully by
creating a (local) temporary layer and copying the features from the
database to the new layer.  Unfortunately, this does not meet my
requirements since any changes to the database are not updated in the
local layer (at least not without regenerating the local layer).  I have
also been able to programmtically add the database to the local catalog
as an available data source.
 
So, my question is this, "How do I programmatically 'reset' the database
connection in the catalog view (to pick up any new tables) and call 'add
to current map' for a specified table (layer)?".

Thanks,

MM

I am currently working with uDig 1.1.1 SDK and eclipse PDE 3.5 (Galileo)
_______________________________________________
User-friendly Desktop Internet GIS (uDig)
http://udig.refractions.net
http://lists.refractions.net/mailman/listinfo/udig-devel
Reply | Threaded
Open this post in threaded view
|

Re: How do I programmatically add a layer from a PostGIS datastore?

Mark Mathis
Any help on this?  It seems like a pretty simple thing to do...

Also, the PostGIS datastore (via the catalog) does not seem to work with
the enterprise db version of postgres.  EnterpriseDB has a separate jdbc
connector, which seems to be required when connecting to the database
from elsewhere.  In the case of uDig, I can connect to the database as
is, but do not see any layers.  I could replace the postgres jdbc driver
with the edb jar if I could find it...  The only reference I can find is
in the pom for gt2-postgis.jar.  Any ideas?

Mark Mathis wrote:

> All,
>
> I am currently trying to develop a uDig custom application.  I have a
> set of existing plugins that create or modify data in a PostGIS
> database (behind the scenes using straight jdbc operations).  I can
> see the results of my computations by manually refreshing the catalog
> (assuming I have already connected to the postgres datastore) and
> using other built in user interface functionality to add/remove layers
> and redraw the map.
> What I would like to do is, given the appropriate connection
> parameters (host, database, table, and optional filter),
> programmatically add a new layer to the map.  I have done this (more
> or less) successfully by creating a (local) temporary layer and
> copying the features from the database to the new layer.  
> Unfortunately, this does not meet my requirements since any changes to
> the database are not updated in the local layer (at least not without
> regenerating the local layer).  I have also been able to
> programmtically add the database to the local catalog as an available
> data source.
>
> So, my question is this, "How do I programmatically 'reset' the
> database connection in the catalog view (to pick up any new tables)
> and call 'add to current map' for a specified table (layer)?".
>
> Thanks,
>
> MM
>
> I am currently working with uDig 1.1.1 SDK and eclipse PDE 3.5 (Galileo)
> _______________________________________________
> User-friendly Desktop Internet GIS (uDig)
> http://udig.refractions.net
> http://lists.refractions.net/mailman/listinfo/udig-devel
_______________________________________________
User-friendly Desktop Internet GIS (uDig)
http://udig.refractions.net
http://lists.refractions.net/mailman/listinfo/udig-devel
Reply | Threaded
Open this post in threaded view
|

Re: How do I programmatically add a layer from a PostGIS datastore?

Mark Mathis
FWIW, the "geometry_columns" and "spatial_ref_sys" tables were owned by
"postgres" not the user we were trying to connect to the database with.  
Once we fixed the permissions on those two tables, uDig was able to
connect to the PostGIS datastore without any difficulty.

I am still trying to figure out how to accomplish my original question,
"How do I programmatically add a layer from a PostGIS datastore?".  This
can't be too hard.  Can anyone point me in the right direction?

Mark Mathis wrote:

> Any help on this?  It seems like a pretty simple thing to do...
>
> Also, the PostGIS datastore (via the catalog) does not seem to work
> with the enterprise db version of postgres.  EnterpriseDB has a
> separate jdbc connector, which seems to be required when connecting to
> the database from elsewhere.  In the case of uDig, I can connect to
> the database as is, but do not see any layers.  I could replace the
> postgres jdbc driver with the edb jar if I could find it...  The only
> reference I can find is in the pom for gt2-postgis.jar.  Any ideas?
>
> Mark Mathis wrote:
>> All,
>>
>> I am currently trying to develop a uDig custom application.  I have a
>> set of existing plugins that create or modify data in a PostGIS
>> database (behind the scenes using straight jdbc operations).  I can
>> see the results of my computations by manually refreshing the catalog
>> (assuming I have already connected to the postgres datastore) and
>> using other built in user interface functionality to add/remove
>> layers and redraw the map.
>> What I would like to do is, given the appropriate connection
>> parameters (host, database, table, and optional filter),
>> programmatically add a new layer to the map.  I have done this (more
>> or less) successfully by creating a (local) temporary layer and
>> copying the features from the database to the new layer.  
>> Unfortunately, this does not meet my requirements since any changes
>> to the database are not updated in the local layer (at least not
>> without regenerating the local layer).  I have also been able to
>> programmtically add the database to the local catalog as an available
>> data source.
>>
>> So, my question is this, "How do I programmatically 'reset' the
>> database connection in the catalog view (to pick up any new tables)
>> and call 'add to current map' for a specified table (layer)?".
>>
>> Thanks,
>>
>> MM
>>
>> I am currently working with uDig 1.1.1 SDK and eclipse PDE 3.5 (Galileo)
>> _______________________________________________
>> User-friendly Desktop Internet GIS (uDig)
>> http://udig.refractions.net
>> http://lists.refractions.net/mailman/listinfo/udig-devel
> _______________________________________________
> User-friendly Desktop Internet GIS (uDig)
> http://udig.refractions.net
> http://lists.refractions.net/mailman/listinfo/udig-devel
_______________________________________________
User-friendly Desktop Internet GIS (uDig)
http://udig.refractions.net
http://lists.refractions.net/mailman/listinfo/udig-devel
Reply | Threaded
Open this post in threaded view
|

Re: How do I programmatically add a layer from a PostGIS datastore?

Andrea Antonello
Some comments, without having tried them though:

Create a postgis service. In PostgisServiceExtension2 there is a nice
cerate metoh you might be able to use.

Add the service to the catalog:

ICatalog catalog = CatalogPlugin.getDefault().getLocalCatalog();
catalog.add(service);


Add then I am going to guess heavily based on what I do with file
based things I use:

Create a PostgisGeoResource2 (pgeoResource) which will represent your
layer and add it to the layers list:

List< ? extends ILayer> addedLayers =
ApplicationGIS.addLayersToMap(map, pgeoResource, layerposition);

That should be more or less what you need.

Hope that helps.

ciao
Andrea



On Thu, Apr 22, 2010 at 11:48 PM, Mark Mathis <[hidden email]> wrote:

> FWIW, the "geometry_columns" and "spatial_ref_sys" tables were owned by
> "postgres" not the user we were trying to connect to the database with.
>  Once we fixed the permissions on those two tables, uDig was able to connect
> to the PostGIS datastore without any difficulty.
>
> I am still trying to figure out how to accomplish my original question, "How
> do I programmatically add a layer from a PostGIS datastore?".  This can't be
> too hard.  Can anyone point me in the right direction?
>
> Mark Mathis wrote:
>>
>> Any help on this?  It seems like a pretty simple thing to do...
>>
>> Also, the PostGIS datastore (via the catalog) does not seem to work with
>> the enterprise db version of postgres.  EnterpriseDB has a separate jdbc
>> connector, which seems to be required when connecting to the database from
>> elsewhere.  In the case of uDig, I can connect to the database as is, but do
>> not see any layers.  I could replace the postgres jdbc driver with the edb
>> jar if I could find it...  The only reference I can find is in the pom for
>> gt2-postgis.jar.  Any ideas?
>>
>> Mark Mathis wrote:
>>>
>>> All,
>>>
>>> I am currently trying to develop a uDig custom application.  I have a set
>>> of existing plugins that create or modify data in a PostGIS database (behind
>>> the scenes using straight jdbc operations).  I can see the results of my
>>> computations by manually refreshing the catalog (assuming I have already
>>> connected to the postgres datastore) and using other built in user interface
>>> functionality to add/remove layers and redraw the map.
>>> What I would like to do is, given the appropriate connection parameters
>>> (host, database, table, and optional filter), programmatically add a new
>>> layer to the map.  I have done this (more or less) successfully by creating
>>> a (local) temporary layer and copying the features from the database to the
>>> new layer.  Unfortunately, this does not meet my requirements since any
>>> changes to the database are not updated in the local layer (at least not
>>> without regenerating the local layer).  I have also been able to
>>> programmtically add the database to the local catalog as an available data
>>> source.
>>>
>>> So, my question is this, "How do I programmatically 'reset' the database
>>> connection in the catalog view (to pick up any new tables) and call 'add to
>>> current map' for a specified table (layer)?".
>>>
>>> Thanks,
>>>
>>> MM
>>>
>>> I am currently working with uDig 1.1.1 SDK and eclipse PDE 3.5 (Galileo)
>>> _______________________________________________
>>> User-friendly Desktop Internet GIS (uDig)
>>> http://udig.refractions.net
>>> http://lists.refractions.net/mailman/listinfo/udig-devel
>>
>> _______________________________________________
>> User-friendly Desktop Internet GIS (uDig)
>> http://udig.refractions.net
>> http://lists.refractions.net/mailman/listinfo/udig-devel
>
> _______________________________________________
> User-friendly Desktop Internet GIS (uDig)
> http://udig.refractions.net
> http://lists.refractions.net/mailman/listinfo/udig-devel
>
_______________________________________________
User-friendly Desktop Internet GIS (uDig)
http://udig.refractions.net
http://lists.refractions.net/mailman/listinfo/udig-devel
Reply | Threaded
Open this post in threaded view
|

Re: How do I programmatically add a layer from a PostGIS datastore?

Jody Garnett-2
In reply to this post by Mark Mathis
Are you looking for DataStore.createSchema( SimpleFeatureType )?
Resolve your service to the DataStore api and then try the above method; you may need to "Reset" the service in uDig or otherwise ask it to notice the new table.

Jody

On 23/04/2010, at 7:48 AM, Mark Mathis wrote:

> FWIW, the "geometry_columns" and "spatial_ref_sys" tables were owned by "postgres" not the user we were trying to connect to the database with.  Once we fixed the permissions on those two tables, uDig was able to connect to the PostGIS datastore without any difficulty.
>
> I am still trying to figure out how to accomplish my original question, "How do I programmatically add a layer from a PostGIS datastore?".  This can't be too hard.  Can anyone point me in the right direction?
>
> Mark Mathis wrote:
>> Any help on this?  It seems like a pretty simple thing to do...
>>
>> Also, the PostGIS datastore (via the catalog) does not seem to work with the enterprise db version of postgres.  EnterpriseDB has a separate jdbc connector, which seems to be required when connecting to the database from elsewhere.  In the case of uDig, I can connect to the database as is, but do not see any layers.  I could replace the postgres jdbc driver with the edb jar if I could find it...  The only reference I can find is in the pom for gt2-postgis.jar.  Any ideas?
>>
>> Mark Mathis wrote:
>>> All,
>>>
>>> I am currently trying to develop a uDig custom application.  I have a set of existing plugins that create or modify data in a PostGIS database (behind the scenes using straight jdbc operations).  I can see the results of my computations by manually refreshing the catalog (assuming I have already connected to the postgres datastore) and using other built in user interface functionality to add/remove layers and redraw the map.
>>> What I would like to do is, given the appropriate connection parameters (host, database, table, and optional filter), programmatically add a new layer to the map.  I have done this (more or less) successfully by creating a (local) temporary layer and copying the features from the database to the new layer.  Unfortunately, this does not meet my requirements since any changes to the database are not updated in the local layer (at least not without regenerating the local layer).  I have also been able to programmtically add the database to the local catalog as an available data source.
>>>
>>> So, my question is this, "How do I programmatically 'reset' the database connection in the catalog view (to pick up any new tables) and call 'add to current map' for a specified table (layer)?".
>>>
>>> Thanks,
>>>
>>> MM
>>>
>>> I am currently working with uDig 1.1.1 SDK and eclipse PDE 3.5 (Galileo)
>>> _______________________________________________
>>> User-friendly Desktop Internet GIS (uDig)
>>> http://udig.refractions.net
>>> http://lists.refractions.net/mailman/listinfo/udig-devel
>> _______________________________________________
>> User-friendly Desktop Internet GIS (uDig)
>> http://udig.refractions.net
>> http://lists.refractions.net/mailman/listinfo/udig-devel
> _______________________________________________
> User-friendly Desktop Internet GIS (uDig)
> http://udig.refractions.net
> http://lists.refractions.net/mailman/listinfo/udig-devel

_______________________________________________
User-friendly Desktop Internet GIS (uDig)
http://udig.refractions.net
http://lists.refractions.net/mailman/listinfo/udig-devel
Reply | Threaded
Open this post in threaded view
|

Re: How do I programmatically add a layer from a PostGIS datastore?

Mark Mathis
Success! (after updating to udig-1.2-RC2-sdk)

            java.util.Map params = new java.util.HashMap();

            params.put(PostgisNGDataStoreFactory.DBTYPE.key, "postgisng");
            params.put(PostgisNGDataStoreFactory.HOST.key, tuple.getHost());
            params.put(PostgisNGDataStoreFactory.PORT.key, tuple.getPort());
            params.put(PostgisNGDataStoreFactory.SCHEMA.key,
tuple.getSchema());
            params.put(PostgisNGDataStoreFactory.DATABASE.key,
tuple.getDatabase());
            params.put(PostgisNGDataStoreFactory.USER.key,
tuple.getUsername());
            params.put(PostgisNGDataStoreFactory.PASSWD.key,
tuple.getPassword());

            PostgisServiceExtension2 postgisServiceExtension = new
PostgisServiceExtension2();
            PostgisService2 postgisService =
postgisServiceExtension.createService(null, params);

            ICatalog catalog = CatalogPlugin.getDefault().getLocalCatalog();
            catalog.add(postgisService);
                                       
            String schema = tuple.getSchema();
            PostgisSchemaFolder postgisSchemaFolder = new
PostgisSchemaFolder(postgisService, schema);
           
            String typename = tuple.getTable();
            IGeoResource postgisGeoResource = new
PostgisGeoResource2(postgisService, postgisSchemaFolder, typename);
           
            IMap map = ApplicationGIS.getActiveMap();
            ApplicationGIS.addLayersToMap(map,
Arrays.asList(postgisGeoResource), -1);

Where "tuple" contains the appropriate connection parameters.

However, this does not work correctly until the DataStore has been
"Reset" from uDig (as Jody suggests).  I could not find a "Reset" method
in any of the pertinent classes.  How do I "otherwise ask it to notice
the new table"?

Thanks!

Jody Garnett wrote:

> Are you looking for DataStore.createSchema( SimpleFeatureType )?
> Resolve your service to the DataStore api and then try the above method; you may need to "Reset" the service in uDig or otherwise ask it to notice the new table.
>
> Jody
>
> On 23/04/2010, at 7:48 AM, Mark Mathis wrote:
>
>  
>> FWIW, the "geometry_columns" and "spatial_ref_sys" tables were owned by "postgres" not the user we were trying to connect to the database with.  Once we fixed the permissions on those two tables, uDig was able to connect to the PostGIS datastore without any difficulty.
>>
>> I am still trying to figure out how to accomplish my original question, "How do I programmatically add a layer from a PostGIS datastore?".  This can't be too hard.  Can anyone point me in the right direction?
>>
>> Mark Mathis wrote:
>>    
>>> Any help on this?  It seems like a pretty simple thing to do...
>>>
>>> Also, the PostGIS datastore (via the catalog) does not seem to work with the enterprise db version of postgres.  EnterpriseDB has a separate jdbc connector, which seems to be required when connecting to the database from elsewhere.  In the case of uDig, I can connect to the database as is, but do not see any layers.  I could replace the postgres jdbc driver with the edb jar if I could find it...  The only reference I can find is in the pom for gt2-postgis.jar.  Any ideas?
>>>
>>> Mark Mathis wrote:
>>>      
>>>> All,
>>>>
>>>> I am currently trying to develop a uDig custom application.  I have a set of existing plugins that create or modify data in a PostGIS database (behind the scenes using straight jdbc operations).  I can see the results of my computations by manually refreshing the catalog (assuming I have already connected to the postgres datastore) and using other built in user interface functionality to add/remove layers and redraw the map.
>>>> What I would like to do is, given the appropriate connection parameters (host, database, table, and optional filter), programmatically add a new layer to the map.  I have done this (more or less) successfully by creating a (local) temporary layer and copying the features from the database to the new layer.  Unfortunately, this does not meet my requirements since any changes to the database are not updated in the local layer (at least not without regenerating the local layer).  I have also been able to programmtically add the database to the local catalog as an available data source.
>>>>
>>>> So, my question is this, "How do I programmatically 'reset' the database connection in the catalog view (to pick up any new tables) and call 'add to current map' for a specified table (layer)?".
>>>>
>>>> Thanks,
>>>>
>>>> MM
>>>>
>>>> I am currently working with uDig 1.1.1 SDK and eclipse PDE 3.5 (Galileo)
>>>> _______________________________________________
>>>> User-friendly Desktop Internet GIS (uDig)
>>>> http://udig.refractions.net
>>>> http://lists.refractions.net/mailman/listinfo/udig-devel
>>>>        
>>> _______________________________________________
>>> User-friendly Desktop Internet GIS (uDig)
>>> http://udig.refractions.net
>>> http://lists.refractions.net/mailman/listinfo/udig-devel
>>>      
>> _______________________________________________
>> User-friendly Desktop Internet GIS (uDig)
>> http://udig.refractions.net
>> http://lists.refractions.net/mailman/listinfo/udig-devel
>>    
>
> _______________________________________________
> User-friendly Desktop Internet GIS (uDig)
> http://udig.refractions.net
> http://lists.refractions.net/mailman/listinfo/udig-devel
>  
_______________________________________________
User-friendly Desktop Internet GIS (uDig)
http://udig.refractions.net
http://lists.refractions.net/mailman/listinfo/udig-devel
Reply | Threaded
Open this post in threaded view
|

Re: How do I programmatically add a layer from a PostGIS datastore?

Jody Garnett-2
The reset action performs a catalog "replace" (replacing the old service with a new one you just made).

I am keen to ask postgis service clear its cache in a less heavy handed fashion; do you have any ideas?
Of importance is that we want the datastore to "rescan" for new tables; perhaps you could check if there is a method for that...

Glad you are making somewhere,

Jody
On 24/04/2010, at 3:47 AM, Mark Mathis wrote:

> Success! (after updating to udig-1.2-RC2-sdk)
>
>           java.util.Map params = new java.util.HashMap();
>
>           params.put(PostgisNGDataStoreFactory.DBTYPE.key, "postgisng");
>           params.put(PostgisNGDataStoreFactory.HOST.key, tuple.getHost());
>           params.put(PostgisNGDataStoreFactory.PORT.key, tuple.getPort());
>           params.put(PostgisNGDataStoreFactory.SCHEMA.key, tuple.getSchema());
>           params.put(PostgisNGDataStoreFactory.DATABASE.key, tuple.getDatabase());
>           params.put(PostgisNGDataStoreFactory.USER.key, tuple.getUsername());
>           params.put(PostgisNGDataStoreFactory.PASSWD.key, tuple.getPassword());
>
>           PostgisServiceExtension2 postgisServiceExtension = new PostgisServiceExtension2();
>           PostgisService2 postgisService = postgisServiceExtension.createService(null, params);
>
>           ICatalog catalog = CatalogPlugin.getDefault().getLocalCatalog();
>           catalog.add(postgisService);
>                                                 String schema = tuple.getSchema();
>           PostgisSchemaFolder postgisSchemaFolder = new PostgisSchemaFolder(postgisService, schema);
>                     String typename = tuple.getTable();
>           IGeoResource postgisGeoResource = new PostgisGeoResource2(postgisService, postgisSchemaFolder, typename);
>                     IMap map = ApplicationGIS.getActiveMap();
>           ApplicationGIS.addLayersToMap(map, Arrays.asList(postgisGeoResource), -1);
>
> Where "tuple" contains the appropriate connection parameters.
> However, this does not work correctly until the DataStore has been "Reset" from uDig (as Jody suggests).  I could not find a "Reset" method in any of the pertinent classes.  How do I "otherwise ask it to notice the new table"?
>
> Thanks!
>
> Jody Garnett wrote:
>> Are you looking for DataStore.createSchema( SimpleFeatureType )?
>> Resolve your service to the DataStore api and then try the above method; you may need to "Reset" the service in uDig or otherwise ask it to notice the new table.
>>
>> Jody
>>
>> On 23/04/2010, at 7:48 AM, Mark Mathis wrote:
>>
>>  
>>> FWIW, the "geometry_columns" and "spatial_ref_sys" tables were owned by "postgres" not the user we were trying to connect to the database with.  Once we fixed the permissions on those two tables, uDig was able to connect to the PostGIS datastore without any difficulty.
>>>
>>> I am still trying to figure out how to accomplish my original question, "How do I programmatically add a layer from a PostGIS datastore?".  This can't be too hard.  Can anyone point me in the right direction?
>>>
>>> Mark Mathis wrote:
>>>    
>>>> Any help on this?  It seems like a pretty simple thing to do...
>>>>
>>>> Also, the PostGIS datastore (via the catalog) does not seem to work with the enterprise db version of postgres.  EnterpriseDB has a separate jdbc connector, which seems to be required when connecting to the database from elsewhere.  In the case of uDig, I can connect to the database as is, but do not see any layers.  I could replace the postgres jdbc driver with the edb jar if I could find it...  The only reference I can find is in the pom for gt2-postgis.jar.  Any ideas?
>>>>
>>>> Mark Mathis wrote:
>>>>      
>>>>> All,
>>>>>
>>>>> I am currently trying to develop a uDig custom application.  I have a set of existing plugins that create or modify data in a PostGIS database (behind the scenes using straight jdbc operations).  I can see the results of my computations by manually refreshing the catalog (assuming I have already connected to the postgres datastore) and using other built in user interface functionality to add/remove layers and redraw the map.
>>>>> What I would like to do is, given the appropriate connection parameters (host, database, table, and optional filter), programmatically add a new layer to the map.  I have done this (more or less) successfully by creating a (local) temporary layer and copying the features from the database to the new layer.  Unfortunately, this does not meet my requirements since any changes to the database are not updated in the local layer (at least not without regenerating the local layer).  I have also been able to programmtically add the database to the local catalog as an available data source.
>>>>>
>>>>> So, my question is this, "How do I programmatically 'reset' the database connection in the catalog view (to pick up any new tables) and call 'add to current map' for a specified table (layer)?".
>>>>>
>>>>> Thanks,
>>>>>
>>>>> MM
>>>>>
>>>>> I am currently working with uDig 1.1.1 SDK and eclipse PDE 3.5 (Galileo)
>>>>> _______________________________________________
>>>>> User-friendly Desktop Internet GIS (uDig)
>>>>> http://udig.refractions.net
>>>>> http://lists.refractions.net/mailman/listinfo/udig-devel
>>>>>        
>>>> _______________________________________________
>>>> User-friendly Desktop Internet GIS (uDig)
>>>> http://udig.refractions.net
>>>> http://lists.refractions.net/mailman/listinfo/udig-devel
>>>>      
>>> _______________________________________________
>>> User-friendly Desktop Internet GIS (uDig)
>>> http://udig.refractions.net
>>> http://lists.refractions.net/mailman/listinfo/udig-devel
>>>    
>>
>> _______________________________________________
>> User-friendly Desktop Internet GIS (uDig)
>> http://udig.refractions.net
>> http://lists.refractions.net/mailman/listinfo/udig-devel
>>  
> _______________________________________________
> User-friendly Desktop Internet GIS (uDig)
> http://udig.refractions.net
> http://lists.refractions.net/mailman/listinfo/udig-devel

_______________________________________________
User-friendly Desktop Internet GIS (uDig)
http://udig.refractions.net
http://lists.refractions.net/mailman/listinfo/udig-devel