Archive for the ‘c#’ Category

SqlGeometry and POINT EMPTY in WKB

Friday, February 26th, 2010

Inspired by question Paul Ramsey asked today morning on IRC, I’ve inspected what kind of Well-Known-Binary output gives SqlGeometry for EMPTY geometries of all the seven geometry types as specified in OGC SFS. The SqlGeometry class is available from SQL Server System CLR Types for .NET Framework. Here we go.

I checked Well-Known-Binary output as returned by the SqlGeometry method STAsBinary(). Here is a small test program written in C#:

using System;
using System.Linq;
using Microsoft.SqlServer.Types;
namespace SqlGeometryEmpty
{
  class Test
  {
    static void Main(string[] args)
    {
      foreach (string type in
         Enum.GetNames(typeof(OpenGisGeometryType)))
      {
        string wkt = type.ToUpper() + " EMPTY";
        SqlGeometry geom = SqlGeometry.Parse(wkt);
        byte[] wkb = geom.STAsBinary().Buffer;
        string wkbhex = string.Join("",
          wkb.Select(
            b => b.ToString("X2")).ToArray());

        Console.WriteLine("{0}\n{1} ({2} bytes)\n",
          wkt, wkbhex, wkb.Length);
      }
    }
  }
}

The first observation is that WKB of EMPTY geometry for all types is returned as a a slightly different binary. All the binary forms are truncated to nine bytes. The first byte indicates endianness as expected. The second chunk of four bytes indicate geometry type. It is exactly as defined in OGC specifications. The third chunk of remaining four bytes are set to Zero and seem to play a role of size specifier: number of points in LINESTRING or number of rings in POLYGON, number of points in MULTIPOINT, and so on. This makes another observation that WKB for EMPTY is reported as a collection of primitive components.

The difference in binary of WKB of EMPTY geometry I mentioned is in that the actual type of input geometry is preserved, so there seems to be no implicit translation to geometry of some other type.

So far so good but not for too long. In fact, SqlGeometry implicitly casts POINT EMPTY to MULTIPOINT EMPTY geometry with the WKB of the following form (in hex):

010400000000000000

Here is complete output of the test program above:

POINT EMPTY
010400000000000000 (9 bytes)

LINESTRING EMPTY
010200000000000000 (9 bytes)

POLYGON EMPTY
010300000000000000 (9 bytes)

MULTIPOINT EMPTY
010400000000000000 (9 bytes)

MULTILINESTRING EMPTY
010500000000000000 (9 bytes)

MULTIPOLYGON EMPTY
010600000000000000 (9 bytes)

GEOMETRYCOLLECTION EMPTY
010700000000000000 (9 bytes)

A word about how PostGIS behaves. PostGIS reports GEOMETRYCOLLECTION EMPTY, regardless of actual type of input EMPTY geometry. It is in hex form:

010700000000000000

Generally, there is not many choices of how to report EMPTY geometry in clear and usable way and a form of collection with size equal to Zero seems to be the most appropriate choice. POINT EMPTY reported with type set to POINT (010100000000000000) would be ambiguous as feels like truncated or invalid form of POINT(0 0), especially in programming languages like C where native dynamic allocated arrays do not carry information about their size. IOW, geometry type is not enough information to process binary form of POINT EMPTY properly.

Reporting EMPTY geometries as a collection is a useful convention that seems to work well. PostGIS behaves about it in the very consistent manner reporting one type for all empties. SqlGeometry, so SQL Server, forces programmers to write a few more lines of code to handle all the possible cases. Yet another original exotic solution from Microsoft.

Consistent API is a bless!

Update: consistent specification of interface is even better.

Tamas Szekeres joins Planet OSGeo

Sunday, January 31st, 2010

OSGeo FoundationThe Planet OSGeo is growing. Today, on behalf of the OSGeo Community, I’m happy to announce Tamas has joined with his blog Sharpening GIS at Your Will.

Tamas has been a contributor to GDAL and MapServer projects for years. He develops and maintains .NET/C# interfaces for both of the projects. He also is a member of the Project Steering Committee for GDAL project.

Welcome Tamas!

Online Compilers

Saturday, September 26th, 2009

Sometimes I need to compile and run a tiny snippet of C++ code. For example, I want to proof some concept or I want to give code example while chatting with friends. Sometimes I don’t have access to good compiler. If I’m logged to irc.freenode.net, it’s not a problem – geordi is there. I’ve just found a Web-based alternative – codepad.org. It is a pastebin service that can compile, validate and execute your code.

Hello World!

Hi COM! How’s it going?

Saturday, May 9th, 2009

In June 2000 at PDC in Orlando Microsoft announced .NET Framework project. Shortly after that developers concentrated around products and technologies made by Microsoft started debate on what’s the future of software development. One of questions and quite obvious was: So does this mean that COM is dead?. A few months later, Don Box addressed that concern in his extensive article Is COM Dead?:

It depends on what your definition of COM is. COM is many things to many people. To me, COM is a programming model based on integrating components based on type. Period. This was COM’s primary contribution to the field of component software, and that contribution has changed the way millions of programmers build systems today.

Don is right and the answer is historically and technically correct, however this is an indirect answer that didn’t cheer up at all. Given the closing words in that article:

(…) CLR provides significant benefits to developers who are using COM today. Virtually all aspects of the COM programming model have survived (…) I look at the CLR as breathing new life into the programming model that I’ve spent the last seven years of my life working with, and I know there are other programmers out there who share this sentiment.

overall conclusion becomes more an abstruse philosophy than binary answer. What about all these cryptic terms of Component Object Model technology made by Microsoft, like OLE, ActiveX, DCOM and other? Silent.

Nearly ten years later, developers are still asking the same question (a few days ago, similar discussion took place on ACCU mailing list) about COM. Alf. P. Steinbach gave an interesting answer that, however, sounds very similar to the one given by Don Box:

COM is one of the few successful C++ component technologies, if not the only one (depending on one’s definition of “successful (…) Some reduced and slightly modified versions of COM are used in e.g. Linux user interface and in Firefox browser (XCOM). Original COM itself is however a Windows-specific technology. But while it’s necessarily used to interface to the operating system and at higher levels in e.g. scripting, it’s my impression that it’s now now not much used as a general C++ component technology, i.e., that use of COM is something forced, not something desired and freely chosen.

Even though both are similar, it’s been decade since the question was asked for the first time and now we have 10 years of experience based on observations what has changed after .NET was thrown on the marked. The managed baby seems to be winning the market of Microsoft-oriented (not only, though) developers and users. COM is widely used as components of Windows systems and other already existing software but are new COM servers and objects still being developed? Or, has it started to suffer of COM programmer species extinction?

The www.microsoft.com/COM/ website confirms that:

Microsoft recommends that developers use the .NET Framework rather than COM for new development.

In my humble opinion, Mr. COM is like an old veteran, alive, but most of his comrades-in-arms have died, so he usually goes to pub alone.

Turing award goes to Barbara Liskov

Friday, March 13th, 2009

Learning principles of Object-Oriented Programming, one of the first and very important thing to understand is a definition of subtype. It’s usually not a big problem to explain it correctly and there are a few descriptions dangling around.

Of course, I have my favourite definition of the relation between supertype and subtype. It is called Liskov Substitution Principle. The LSP reveals existing subtleties that may make understanding of the term of subtype not easy. There is a well-known example presenting potential problems: a squere is a rectangle or may be it is not? Robert Martin has written more about in C++ Report long time ago (PDF). For a C++ programmer, like myself – who cares about design by contract (DbC) – the Liskov Substitution Principle is helpful to understand role of pre-/post-conditions in inheritance.

The Liskov Substitution Principle was formulated by Professor Barbara Liskov (MIT). Two days ago, BBC announced as follows:

The 2009 A. M. Turing Award has gone to Barbara Liskov for her contributions to programming.

(Barbara Liskov wins Turing award, BBC)

Congratulations!

Write your own map calibration tool

Monday, February 6th, 2006

Yesterday, the CodeProject announced new articles. One of them is a small tutorial about how to write simple map calibration tool in .NETGeo-referencing {Map Calibration} by Mohammad Riazi and H.Riazi.

Besides map calibration technique, this article explains rudiments of coordinates transformation between Geographic coordinates (latitude and longtitude) to Cartesian coordiantes (X, Y) on plane. Along clear explanation a set of simple equations is also provided. In my opinion this article is a good start for beginners of GIS and GPS applications.

Table of Contents has been published

Sunday, January 29th, 2006

Charles Petzold TattooToday, Charles Petzold announced that he has published Table of Contents of his new book:

I’ve created a page on my web site for the new book and included a Table of Contents that goes as far as I know. I hope to keep it updated as the rest of the book becomes firmer in my mind.

Charles Petzold is working on new book Applications = Code + Markup. A Guide to the Microsoft Windows Presentation Foundation and he seems to not to go slow because it’s comming this fall 2006:

Yesterday I hit the 400 book pages mark. My goal of 5 books page per calendar day starting November 1st implies that I’m about a week behind. It appears now that Part 1 of the book (which sticks entirely to C# code) will be about 400 pages long.

If you are interested in what’s new in the book, visit Petzold Book Blog.