Wednesday, January 4, 2017

Fun with Interfaces and Pointers in Go

Now that I am doing server-side programming again, I have fallen in love with the Go language. Long story short, it provides just about everything I need to produce quality server-side code and deliberately withholds language features that have unintended consequences in code quality and maintenance. I believe this has enabled me to produce higher quality code faster than with other languages like Java.

However, it has not always been smooth sailing. I recently hit an unexpected bump in what I thought was fairly simple code. As it turned out, I had not fully grasped how Go handles interfaces and pointers. Allow me to explain the scenario, and in so doing, illustrate how Go interfaces and pointers work. (I only present the minimal code needed to get the point across. Anything extraneous to the point is omitted.)
  1. I wanted to have a custom error struct that had some state information in it. This was done by creating a struct called Error that implements the error interface's one required function, Error().
    type Error struct {
    Message string

    func (e Error) Error() string {
    return e.Message
  2. I wanted Error to lazily initialize its state. For this to work properly (i.e., retain that state after multiple calls to Error()), I needed to use a pointer receiver in my Error() function. Note the difference between the code below and the code above.
    type Error struct {
    Message string

    func (e *Error) Error() string {
    if e.Message == "" {
    e.Message = time.Now().String()
    return e.Message
  3. For functions like foo() that are only ever going to return my own struct, I figured that I could return *Error so that I would have full access to its members without any type checking. (As you will see later, this turned out to be a bad idea!)
    func foo() *Error {
    return nil
  4. Then I could subsequently return the results (an error or nil) to a higher function.
    func bar() error {
    return foo()
  5. Finally I would be able to call my bar function from high level code and check the return value.
    e := bar()
    if e == nil {
    fmt.Println("Hello, playground")
    } else {
    fmt.Println("Message:" + e.Error())

There is just one problem – this code segfaults. Try it out here.

Why this happens is probably not obvious to most inexperienced Go programmers. What is it that implements the error interface? Not Error, but rather *Error. The problem is that bar() must return a) something that implements the error interface or b) nil. Well, *Error implements that interface. As written here (without all of the other logic that makes the function meaningful) foo() returns a pointer to nil. A pointer to nil is not the same as nil. Repeat that to yourself, more than once if you have to.

So what have I done? I created a struct, used a pointer receiver to implement an interface, and called a function that returns a pointer to my struct, and returned that pointer (that just so happens to point to nil). Well crap, since nil is not the same as a pointer to nil the equality expression fails and we try to dereference a nil pointer. Kaboom! The fix is simple – change foo() to return error instead of *Error. Done! (Oh well, my assertion in #3 is wrong and so I will just have to manually check to see if my error happens to be a *Error.)

The moral of the story is that if your struct returns an error, return an error and not something else that implements the error interface. However, sometimes the journey is more important than the destination. You can't fully understand the language if you don't truly understand how the language handles interfaces and pointers.

Tuesday, January 3, 2017

Non-spatial Tables

About a year ago, my Twitter timeline blew up. It turns out the Esri User Conference was going on and this got a lot of people chatting together. The root of the matter was a perception that Esri was not providing reasonable support for non-spatial data within GeoPackages. This was leading people towards flawed, clumsy workarounds like inventing spatial columns to tack onto attribute tables(!). Uh, folks, this is not what we had in mind...

I am not here to point fingers and the fact is that both sides had a point. A strict read of GeoPackage v1.1 did not allow non-spatial attribute values. However, in practice data providers routinely need to deliver data that does not contain geometry properties. We agreed that it was not reasonable to require any GeoPackage that contained non-spatial tables to be declared and documented as an "Extended GeoPackage". This does not promote interoperability. 

In response, we modified the standard by adding a new Attributes section that describes how to store non-spatial attribute tables in a GeoPackage. The new section is present in the on-line (working) copy of the specification and it will be incorporated in the next release (tentatively numbered GeoPackage 1.2). We hope that this addition will clarify things and encourage people to use GeoPackages as intended. We consider the change to be low-risk because it creates a new encoding option that would be ignored by previous versions of the standard.

Wednesday, December 14, 2016

Elevation Extension Update

As previously mentioned, the GeoPackage SWG has been investigating an extension to support tiled, gridded elevation data. A year later, we are pleased to announced that we have completed our work on this extension and that we intend to release it as part of GeoPackage 1.2. This extension will share the mechanism used in the tiles portion of the standard.

The extension description describes the metadata tables needed to manage elevation tiles and encodings for the elevation values themselves. The extension supports two encodings. Most users will use the PNG encoding which uses 16-bit integers. An optional scale and offset allow for more efficient use of the 16-bit space. For those users who require greater resolution, there is a TIFF encoding which uses 32-bit floating point values. For convenience, we also provide abstract tests and table definition SQL.

To ensure that this approach will work, we conducted an interoperability experiment. In this experiment, documented in an Engineering Report[1], a number of participants produced GeoPackages containing elevation data and ran visualization and/or analytics on them. The experiment exposed a number of issues that the SWG subsequently resolved. At the OGC Technical Committee meeting in February, the SWG voted to add the extension to the standard. Do you think it is ready?

[1] At this time this link may only be available to OGC members. When it published on the main site I will update this post.

Tuesday, November 1, 2016

Internal Versioning for GeoPackage Files

We have a tricky situation and I need some feedback from the community to make sure we do it right. What we need is a way to identify the exact version of the GeoPackage in use in a particular file. We have tried to maintain compatibility between versions as much as possible, but there are subtle differences between one version to the next. Most applications handle these differences through duck-typing but conformance tests (like the emerging GeoPackage 1.0 ETS) do not have that option.

A recently opened ticket presents a possible way forward here. The recommendation is to for GeoPackage 1.2 to go back to using the SQLite application_id "GPKG" and to use the SQLite user_version to indicate the GeoPackage version. This would be an improvement over where we were heading - registering a series of application_id values (GP10, GP11...), suggesting completely different and incompatible versions.

There are two possible problems with this approach. The first is that GeoPackage providers are already using the user_version for their own uses. The other possible issue is that early versions of SQLite software don't support that field. We don't know of anyone that would be troubled by either. If you do, please let us know!

Tuesday, June 14, 2016

Maintaining Reverse Compatibility

Right now, OGC's Architecture Board (OAB) is trying to create guidance for describing and maintaining reverse compatibility. The following are my recommendations based on my experience with GeoPackage.

0. (Prime Directive) Do no harm. Practicality should be the most important consideration. Understand what existing implementations are doing so that you do not cause them new problems with the changes. However, do not shackle yourself unnecessarily. With new standards it is not unusual for obscure parts of the standard to be unused operationally. If they are wrong, fix them while you still have a chance.

1. The semantics of existing elements shall not be changed if they are in use operationally. In we did not permit ourselves to change the way the WKT was encoded. Instead we created a new extension which adds a column.

2. New requirements may be added for any reason as long as doing so does not adversely impact interoperability. In we knew we had an interoperability problem. The new requirement corrects that problem but there is nothing we can do about implementers that misinterpreted the (admittedly ambiguous) original text.

3. Existing requirements shall not be changed, relaxed, or removed if existing implementations rely on them being met. In we had a consistency problem that we were able to correct because while existing implemetations were creating the rogue columns, they were not reading or writing to them. In we moved a requirement from core to an extension because it wasn't being used operationally and it was just an annoyance for implementers.

Wednesday, March 23, 2016

Congratulating Paul Daisey

Today we officially celebrate Paul Daisey's retirement. I met Paul in 2009 and we worked together for several years, primarily supporting Army Geospatial Center. I want to share three things that I take away from my time working with him over the last 6-7 years.

1. I have great respect for Paul's efforts as the GeoPackage SWG chair. This role was challenging because of the large number of disparate groups who had strong opinions on how things should be done, particularly on the vector side. It is rare to find someone with the technical acumen, community respect, attention to detail, and diplomacy to get the job done. It is through his efforts that GeoPackage exists today.

2. Sometimes it is the little things. Paul was great at transcribing meetings. You always knew that if Paul was there, he'd make a clear record of who said what. Now whenever I go to a meeting and have to take notes, I always chuckle to myself that I wish Paul were there so that I didn't have to do it and because he could do it better than I could.

3. Paul showed me how I want to go out. Over time, he gradually reduced his hours so that he could spend more time with his family, his home, and his hobbies. If something important and interesting came up, he was available to step up. If there wasn't much going on, it was no big deal. That's how I want to retire. 

Congratulations, Paul.

Tuesday, February 2, 2016

A Clarification - 1.1.0

"I have never been wrong BUT I have been known to clarify my position from time to time." - me
A few months ago I mentioned that I want to avoid substantive changes and a version 1.1 of the GeoPackage standard. It didn't quite work out that way so I would like to clarify what administrative, substantive, and critical changes mean in the context of standards development.
  • Administrative changes are changes to wording, grammar, and usage that do not affect requirements
  • Substantive changes alter requirements
  • Critical changes modify requirements in a way that make existing files invalid or would make valid files unusable on systems designed to work with the original version
As mentioned previously, we made a number of substantive changes in the new version. In general, the goal of these changes is to make the standard better - easier to read, less ambiguous, and more consistent. The OGC Architecture Board (OAB) determined that these substantive changes were sufficient to call this a minor release (1.1) as opposed to a point release (1.0.2). In response, I have updated all relevant text to identify the upcoming release as GeoPackage 1.1.

I am fine with this. What I really should have said in September is that critical changes are to be avoided. I still want to avoid a "GeoPackage 2" or even a "GeoPackage 1.3". The GeoPackage developer community is as adamant about this as I am, if not more so. This was never more apparent than in the discussion about the CRS WKT extension. It took more work to get this extension to the point that it was satisfactory. Maybe now we are where we need to be. If you don't think it is there, let us know.  I want to get this right.