Thinking in GIS

a blog about GIS from a urban geogeek living at the countryside

Feed, Categories, Archives


PostGis WorkspaceFactory

Posted: February 21, 2007
Categories: GIS, .NET, ArcObjects, devs, PostGIS, ArcGis Desktop, ZigGis, COM
Feedback: View Comments

In my previous post I have showed you how to install and using zigGIS for adding PostGIS data into ArcMap.
From the ArcMap user interface you just need to press the zigGIS button and an "Add PostGIS data" dialog will be shown to you. From there you can set a zig File path, where PostGIS connections settings are stored, and then a PostGIS layers list will be shown and you can check which layer(s) add to the map.
Add PostGis data
In this post I will show how to programmatically use zigGIS and ArcObjects to add PostGIS data into ArcMap.
zigGIS is a Microsoft .NET 2.0 library that can be used from .NET clients and also from COM clients.
You could use it in your ArcObjects .NET applications/library to manage PostGIS data, in any .NET language like VB .NET and C#.
As zigGIS it is obviously exposed to COM (ArcObjects are in COM), you could also use it in your COM applications/library, for example in applications developed with Visual Basic 6 and ArcMap's VBA (Visual Basic for Application).
more
zigGIS has a complete Geodatabase implementation for accessing PostGIS OGC Data. For obtaining so a whole set of Geodatabase ArcObjects has been written from scratch, based on the ArcObjects framework and following the Esri standards.
At this time zigGIS consists of 15 ArcObjects Geodatabase derived classes (plus other utilities classes), from which the most impourtant and maybe the only one exposed to a developer willing to make PostGIS customisation with ArcObjects is the PostGisWorkspaceFactory class, a concrete implementation for PostGIS of the WorkspaceFactory abstract class from Esri.

Using the PostGisWorkspaceFactory class (and maybe some other ones like the PostGisEnumDataset class) you will make your ArcObjects customisation PostGIS-Enabled, in a very elegant way totally compliant with the Esri ArcObjects code standards.
For example you could mix PostGisWorkspaceFactory with other WorkspaceFactory concrete class (CadWorkspaceFactory, IMSWorkspaceFactory, SDEWorkspaceFactory...) to make an application that mix GIS data from a large variety of sources.

As you can easily understand from the following image, the PostGisWorkspaceFactory class is just like other xxxWorkspaceFactory classes, making easy for the ArcObjects developer to access in a standard way at many data sources and with zigGIS including now the PostGIS data source.

PostGisWorkspaceFactory
As you - experienced ArcObjects developer - may easily guess, to access to the PostGis data store you will need to create a PostGisWorkspaceFactory and access at it by the Open() or OpenFromFile() methods. The Open method is waiting for an IPropertySet parameters collection defining the PostGIS connection (in the same SDEWorkspaceFactory.Open method fashion). The OpenFromFile method is waiting for a zig file parameter.

Now I will quickly show how to use the zigGIS library for programmatically add a PostGIS layer in C# .NET, VB .NET and VBA.

Adding PostGIS data with ArcObjects and zigGis in .NET

For using the zigGIS library in a .NET IDE like Visual Studio .NET, make sure to add a .NET reference in your project to the zigGIS.dll.
After doing this you should be able to have intellisense for ZigGis classes, and also you should see their properties and methods by the object browser, like showed in the following picture.

netobjectbrowser.gif

In the C# .NET code don't forget importing the needed Esri ArcObjects and zigGIS libraries by the "using" keyword. In the VB .NET code do the same by using the "imports" keyword.

Adding PostGIS data with ArcObjects and zigGis in C# .NET

using ESRI.ArcGIS.Geodatabase;
using ESRI.ArcGIS.esriSystem;
using ESRI.ArcGIS.Carto;
using ESRI.ArcGIS.Display;
using ZigGis.ArcGIS.Geodatabase;

// we suppose to have a MapControl called axMapControl1

// Create PostGisWorkspaceFactory IWorkspaceFactory wksf = new PostGisWorkspaceFactory();

//Open the PostGIS Workspace from a from IPropertySet (in the SDEWorkspaceFactory fashion) IPropertySet ps = new PropertySetClass(); ps.SetProperty("server", "localhost"); ps.SetProperty("database", "TUTORIAL"); ps.SetProperty("user", "psqluser"); ps.SetProperty("password", "psqluser"); ps.SetProperty("port", "5432"); //specify the configfile property if you want to log zigGis actions for debugging //ps.SetProperty("configfile", @"C:ziggisZigGislogging.config"); IWorkspace ws = wksf.Open(ps, 0);

//Alternatively you can open the PostGIS Workspace from a zigFile //ws = wksf.OpenFromFile(@"C:ziggisZigGisexample.zig", 0);

//Open the PostGIS feature class IFeatureWorkspace fwks = ws as IFeatureWorkspace; IFeatureClass fc = fwks.OpenFeatureClass("zone");

//Create the new layer (default renderer is ISimpleRenderer) IFeatureLayer layer = new PostGisFeatureLayer(); layer.FeatureClass = fc; layer.Name = fc.AliasName;

//Add the layer to the Map Control axMapControl1.AddLayer(layer, 0);


Adding PostGIS data with ArcObjects and zigGis in Visual Basic .NET

Imports ESRI.ArcGIS.Geodatabase
Imports ESRI.ArcGIS.esriSystem
Imports ESRI.ArcGIS.Carto
Imports ESRI.ArcGIS.Display
Imports ZigGis.ArcGIS.Geodatabase

'we suppose to have a MapControl called axMapControl1

'Create PostGisWorkspaceFactory Dim wksf As IWorkspaceFactory = New PostGisWorkspaceFactory

'Open the PostGIS Workspace from a from IPropertySet (in the SDEWorkspaceFactory fashion) Dim ps As IPropertySet = New PropertySet With ps .SetProperty("server", "localhost") .SetProperty("database", "TUTORIAL") .SetProperty("user", "psqluser") .SetProperty("password", "psqluser") .SetProperty("port", "5432") 'specify the configfile property if you want to log zigGis actions for debugging '.SetProperty("configfile", @"C:ziggisZigGislogging.config") End With Dim ws As IWorkspace = wksf.Open(ps, 0)

'Alternatively you can open the PostGIS Workspace from a zigFile 'ws = wksf.OpenFromFile(@"C:ziggisZigGisexample.zig", 0);

'Open the PostGIS feature class Dim fwks As IFeatureWorkspace = CType(ws, IFeatureWorkspace) Dim fc As IFeatureClass = fwks.OpenFeatureClass("zone")

'Create the new layer (default renderer is ISimpleRenderer) Dim layer As IFeatureLayer = New PostGisFeatureLayer() layer.FeatureClass = fc layer.Name = fc.AliasName

'Add the layer to the Map Control AxMapControl1.AddLayer(layer, 0)


Adding PostGIS data with ArcObjects and zigGis in ArcMap's VBA


For using the zigGIS library in a COM IDE like Visual Studio 6 or ArcMap's VBA, make sure to add a COM reference in your project to the ZigGis.dll.
After doing this you will have intellisense for ZigGis classes, and you will see their properties and methods by the object browser, like showed in the following image.


'Create PostGisWorkspaceFactory Dim wksf As IWorkspaceFactory Set wksf = New PostGisWorkspaceFactory

'Open the PostGIS Workspace from a from IPropertySet (in the SDEWorkspaceFactory fashion) Dim ps As IPropertySet Set ps = New PropertySet With ps .SetProperty "server", "localhost" .SetProperty "database", "TUTORIAL" .SetProperty "user", "psqluser" .SetProperty "password", "psqluser" .SetProperty "port", "5432" 'specify the configfile property if you want to log zigGis actions for debugging '.SetProperty("configfile", @"C:ziggisZigGislogging.config") End With Dim ws As IWorkspace Set ws = wksf.Open(ps, 0)

'Alternatively you can open the PostGIS Workspace from a zigFile 'ws = wksf.OpenFromFile(@"C:ziggisZigGisexample.zig", 0);

'Open the PostGIS feature class Dim fwks As IFeatureWorkspace Set fwks = ws Dim fc As IFeatureClass Set fc = fwks.OpenFeatureClass("zone")

'Create the new layer (default renderer is ISimpleRenderer) Dim layer As IFeatureLayer Set layer = New PostGisFeatureLayer Set layer.FeatureClass = fc layer.Name = fc.AliasName

'Add the layer in ArcMap focus map Dim pMxDoc As IMxDocument Set pMxDoc = ThisDocument pMxDoc.FocusMap.AddLayer layer

VBA Object Browser

Have fun with your PostGIS ArcObjects customisations!!!

blog comments powered by Disqus