Home > Uncategorized > A generalized grid generator operator for Carmenta Engine

A generalized grid generator operator for Carmenta Engine

Carmenta Engine is a tool box for map visualization. It provides a lot of building blocks that are useful when working with map data visualization and processing. One of the components is called GridGeneratorOp and it is used to generate a grid in any coordinate system. Very useful, indeed, since grids are common in maps. However, if you need the grid to be displayed nicely at a wide range of scale levels, one could say that it does not scale well (please excuse the pun). The reason is that you have to add one operator for each scale level. Luckily, Carmenta Engine has a powerful extension mechanism that, among other things, makes it possible to implement your own operator (this is called a custom operator). So, lets just stop talking and start working. A custom operator needs to implement the interface:

namespace Carmenta.SpatialAce.Interop.CustomObjects
{
    public interface ICustomOperator
    {
        void InitNew(IOperatorContext context);
        ICustomOperator Clone();
        void Prepare(IGeoInfo info);
        void PrepareSpecific(IGeoInfo info, Identities ids);
        bool Get();
    }
}

  • InitNew – initialization hook. Here you have a chance to retrieve values specified in the configuration (.p) file. Make sure you store the context parameter since you will need it later.
  • Clone – a custom operator must be a able to create a copy of itself.
  • Prepare/PrepareSpecific – called once per update. Prepare the operator for a new update cycle.
  • Get – called once per processed object, This is the iteration step where the operator actually performs its work.

So, here is the outline of my “improved” grid generator operator.

namespace GridLibrary
{
    public class ScaleIndependentGridOp : ICustomOperator
    {
        public void InitNew(IOperatorContext context)
        {
            this.context = context;
            refSys = Util.ResolveRefSystemById(Util.GetPropertyValueOrDefault<int>(context, "refSysId", 4326));
            valueAttribute = Util.GetPropertyValueOrDefault<string>(context, "valueAttribute", "value");
            valueAttributeFormat = ValueAttributeFormat.spaceValueFormatString0;
            lineDensity = (double)Util.GetPropertyValueOrDefault<float>(context, "lineDensity", 4.0f);
        }

        public void Prepare(IGeoInfo info)
        {
            var gridOpWrapper = GetOrCreateGridOpWrapper(info);
            objects = gridOpWrapper.Get(info);
            objectCounter = 0;
        }

        public bool Get()
        {
            if (objectCounter < objects.Count)
            {
                context.SetGeoObject(objects[objectCounter++]);
                return true;
            }
            else
            {
                return false;
            }
        }

        private GridOpWrapper GetOrCreateGridOpWrapper(IGeoInfo info)
        { … }
    }
}

In InitNew I simply retrieve the coordinate system (or reference system) that the grid is defined in and the name of the attribute that will hold the x/y coordinate. This is equivalent to the ordinary grid generator operator. I also retrieve a parameter called line density. This is, roughly, the number of grid lines (per axis) that will be displayed.

In prepare, I retrieve an instance of an ordinary grid generator operator, that is specifically configured for the current scale level. I use this operator to produce its grid lines and store them in the objects collection.

In get I simply return all the objects in the objects collection that was created in prepare.

This is the basic idea. Please, feel free to download the source code and dig into the gory details or just simply use it as it is. A sample configuration file (testconfig.p) is also provided along with the source code. It might also we worth noting that the generalized grid operator does not require any post processing by project operators and rectclip operators opposed to the original grid generator.

 

Advertisements
Categories: Uncategorized
  1. Tobias
    April 20, 2010 at 6:56 pm

    Really cool stuff! I\’ll probably use your code as a starting point for my own custom grid generator – a certain Spanish customer has some very specific grid requirements.

  1. No trackbacks yet.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: