Beginning examples

Working with the PluginManager Class

Let start with a simple example. Create a console application project and add references as shown above (Getting started).

using gView.Framework.system;
using gView.Framework.Data;
using gView.Framework.FDB;
using gView.Framework.Geometry;
…
class Program
{
   static void Main(string[] args)
   {
      gViewGIS.Init();

      PlugInManager pm = new PlugInManager();

      #region List all IDataset Plug Ins

      Console.WriteLine("List all IDataset plugins:");
      Console.WriteLine("--------------------------");
      ListPlugins(pm.GetPlugins(typeof(IDataset)));
      Console.WriteLine();
          
      #endregion
    }

   static public void ListPlugins(PlugInManager.PlugInTypeList list)
   {
      foreach (PlugInManager.PlugInType plugInType in list)
      {
         Console.WriteLine("Guid: " + plugInType.PluginGuid.ToString() + 
                                     "\nType: " +       plugInType.Type.ToString());
         Console.WriteLine();
      }
   }
}

In the first line we call the static function gViewGIS.Init(). You need this line, if you create an EXE, that is using the gView-Framework. This function set the working directory of you application to the gView program folder. This is necessary, because the framework need partially access to native libraries (proj, gdal, …) and expect them in the working directory. If you are writing gView plugins, you do not need this line, because plugins must copied in the gView program folder anyway.
In the next line we create an instance of the PluginManager class. It provides access to the gView plugin system. Here we call the GetPlugins method to get an array of PlugInManager.PlugInTypeList objects. The static ListPlugins method of our program write the plugins and their type name to the console.
The GetPlugins method needs on argument: a type of the plugins. In this example, all plugins are listed, that implements the IDataset interface.
For example, to list all datasets, that store features (object with geometry and attributes), we can use this line:

ListPlugins(pm.GetPlugins(typeof(IFeatureDataset)));

All listed feature datasets are readable, but not all are writable. To list only the possible writable feature datasets you can use this code:

PlugInManager.PlugInTypeList writables = new PlugInManager.PlugInTypeList();
foreach (PlugInManager.PlugInType plugInType in pm.GetPlugins(typeof(IFeatureDataset)))
{
    IFeatureDataset featureDataset = (IFeatureDataset)plugInType.CreateInstance();
    // If datasets database object implements 
    // gView.Framework.FDB.IFeatureUpdater its writable
    // -> IFeatureUpdater.Insert, Update and Delete method
    if (featureDataset.Database is IFeatureUpdater)
    {
        writables.Add(plugInType);
    }
}
ListPlugins(writables);

Working with geographic data

The next example lists the featureclasses (database tables containing features) of a dataset (here PostGIS database):

using (IFeatureDataset dataset = 
(IFeatureDataset)pm.CreateInstance(new Guid("206cf40b-d4d9-4e85-b872-d2e63c3556ba")))
{
    dataset.ConnectionString = "Server=localhost;Port=5432;Userid=***;Password=***;Database=postgis201;Protocol=3;SSL=true; Pooling=true;MinPoolSize=3;MaxPoolSize=20;Encoding=UNICODE;Timeout=20;SslMode=Disable;";
    dataset.Open();
    foreach (IDatasetElement element in dataset.Elements)
    {
        IFeatureClass fc = element.Class as IFeatureClass;
        if (fc == null) continue;
        Console.WriteLine(fc.Name+": "+fc.CountFeatures+" Features");
     }
}

In the first line, we are creating an instance of a specific feature dataset object. To do this, you must know, that every gView plugin has a unique GUID to be identified. In this case we are creating a PostGIS dataset. The get the GUID of a plugin you can simple use the Plugin-Manager in the gView.Desktop.Carto.exe:

pluginmanager.png

After creating an instance of a feature dataset, we assign the connection string and open the dataset. With the dataset property Elements, we get a list of all the elements in the dataset. Every IDatasetElement object has a Class property. We can check this property: if it implements the IFeatureClass interface, we can, for example, query the number of features in the featureclass.
The next example is more advanced. Loop through all features in a featureclass and perform a geographic projection on the features geometry.

using (IFeatureDataset dataset = 
(IFeatureDataset)pm.CreateInstance(new Guid("33254063-133d-4b17-aae2-46af7a7da733")))
{
    dataset.ConnectionString = "Server=localhost;Port=5432;Userid=***;Password=***;Database=fdb;Protocol=3;SSL=true; Pooling=true;MinPoolSize=3;MaxPoolSize=20;Encoding=UNICODE;Timeout=20;SslMode=Disable;dsname=OSM";
    dataset.Open();
    IFeatureClass fc =  (IFeatureClass)dataset["waterways"].Class;

    QueryFilter filter = new QueryFilter();
    filter.AddField(fc.ShapeFieldName);

    using (GeometricTransformer transformer = new GeometricTransformer())
    {
        transformer.SetSpatialReferences(fc.SpatialReference,
        SpatialReference.FromID("epsg:31256"));
        using (IFeatureCursor cursor = fc.GetFeatures(filter))
        {
             IFeature feature;
             while ((feature = cursor.NextFeature) != null)
             {
                     IGeometry shape = (IGeometry)transformer.Transform2D(feature.Shape);
                     Console.WriteLine(shape.Envelope.ToBBoxString());
             }
         }
     }
} 

So you can see how powerful the framework is. Its easy to read geographic data and project it. In the next step I will show an example for reading features and write them to another dataset.
(Coming Soon)



Last edited Nov 9, 2012 at 5:18 PM by gisdev1, version 18

Comments

No comments yet.