Reading and writing Esri shapefiles in Delphi

 Delphi, GIS  Comments Off on Reading and writing Esri shapefiles in Delphi
Jan 212017
 

Esri Shapefile is a popular file format in geographic information systems (GIS). Probably because the format has been extensively documented (whitepaper in PDF format).

In my day job at a company that does road condition surveys I work with quite a lot of data in shapefiles format, so in order to read and write them I wrote two Delphi units. These units have been in existence for quite a while but until recently they could only process rather basic file types with points and polylines. After having the need to process additional types, I have updated (and bugfixed) them, so they now can read the following shape types:

  • Point, PointZ, PointM
  • Polygon, PolygonZ, PolygonM
  • Polyline, PolylineZ, PolylineM
  • MultiPoint, MultiPointZ, MultiPointM

Please note that they only accesses the .shp file, not the accompanying .dbf, .proj, .shx and other files. The .dbf files are simply dbase files, so you could use e.g. tdbf for them (But check the tdbf license!). The .proj files are text.

The writer is more basic yet, it can only write Point shapes.

Both are part of dzlib, my general purpose library, and available under the Mozilla Public License from the svn repository on SourceForge:

dzEditorLineEndsFix 1.0.3 released

 Delphi, Windows, Windows 8.1  Comments Off on dzEditorLineEndsFix 1.0.3 released
Jan 212017
 

I have released a new version of my dzEditorLineEndsFix tool for Delphi 2006 to 2010. There is only one change: I removed the balloon hint it used to show at startup. It started to annoy the hell out of me (and I’m probably not the only one).

The tool also now has its own page on this blog.

GExperts 1.38 experimental twm 2017-01-15 released

 Delphi, GExperts  Comments Off on GExperts 1.38 experimental twm 2017-01-15 released
Jan 152017
 

I has been a while since my last experimental GExperts release. Erik still hasn’t managed to do the final official release and I don’t want to wait any longer. I have created installers for all supported Delphi versions.

New features include:

There was also a fix for a nasty bug in the code formatter.

Did I ever blog about using the .MAP file for the Uses Clause Manager and Open File Expert? This was also added in this version (with using it in grep on the todo list). Also, did I ever mention the new Editor Experts: WARN directive and IF Directive? I don’t remember but they were added months ago.

Please be aware that I mostly work with Delphi 2007, so this version can be regarded as tested quite well, followed by Delphi XE2. The others are only known to compile and new features are usually tested superficially with all versions. This is particularly true for Delphi 6/7 and 2005/2006.

Head over to the Experimental GExperts page to download the latest release it.

GExperts now adds favorites to the Build Events

 Delphi, GExperts  Comments Off on GExperts now adds favorites to the Build Events
Jan 152017
 

I use build events in Delphi programming to do various things, but usually it’s this:

  • pre-build: Increment the build number (in an external ini file) and create a version resource from that info.
  • post-build: Add translations and jcldebug information to the executable.

For each new program and quite a few older programs which don’t do this yet, this means I have to add these build events. But since I don’t do it frequently enough to know them by heart, I have to look these commands up every time. And it’s still often enough for it to be a nuisance, so I have now spent an outrageous amount of time (in comparison to the time it takes to add these two calls to any Delphi project) hacking the Delphi IDE and adding a Favorites button to all the dialogs in question (in all Delphi versions supported by GExperts).

Here is the result:

This button is now available in the Build Event dialog for all types of build events:

In addition, its available directly on the page where you set up the build events in the Project Options dialog of Delphi 2007 to XE (later versions use that annoying string grid for the build events so it wasn’t possible to simply add a button):

Of course there is a corresponding configuration option to en-/disable this IDE enhancement (even though I can’t think of a reason why anybody wouldn’t want it):


I’m thinking about doing another experimental GExperts release in the near future. Until then you have to get the sources and build your own dll as described in my article on How to compile GExperts. Be sure to get the source from the Formatter branch, as described there.

Ok, there it is, a new GExperts release that includes this feature.

Hiding the Quick Action and Description panel in Delphi with GExperts

 Delphi, GExperts  Comments Off on Hiding the Quick Action and Description panel in Delphi with GExperts
Jan 082017
 

Delphi 2010 introduced two new panels at the bottom of the Object Inspector in the Delphi IDE, the Quick Action panel and the Description panel.

I didn’t really notice them until I read this question on StackOverflow. It caught my attention because the screenshot showed mostly captions of the menu items added by GExperts to the context menu of the form designer.

MartynA showed a way to hide them in his answer and gave me permission to use his code in GExperts. Unfortunately it had a small flaw:

… set the Height of the THotCommands and TDescriptionPane controls to 0, because the IDE seems to reset their Visible property to True after the component selection in the OI is changed. Fortunately, whatever code does that does not also reset their Heights to a non-zero value.

I didn’t like his workaround, but I had a brilliant™ idea: Instead of simply hiding these controls I introduced a new, hidden panel and changed their Parent to this panel. Problem solved, not. There were also two TSplitter controls which were left visible. Moving them too hid them just fine.

The next problem was making them visible again. Simply moving them back, left the controls in the wrong order. The splitters were below both panels rather than one above each. It took me a while to get this right. The solution was to always first move all controls to my hidden panel and then move them back in bottom up order.

Then it was “just” a matter of adding some configuration options and setting the right IFDEFS (Delphi 10 introduced the option to hide these panels from the Object Inspector’s context menu.). And voila, a new IDE enhancement was born.

About DBase string fields

 dBase  Comments Off on About DBase string fields
Dec 142016
 

Today I got a Dbase table that was odd:
When read using tdbf, some string field contained NULL in one record, but an empty string in another record. I was unable to produce such a file with Personal Dbase 8 as well as our internal tool DBaseTableViewer (which despite its name can also change tables, it started out as a simple viewer and uses tdbf to access the files): Deleting the contents of a string field always resulted in NULL. Nonetheless the dbf file looked like this:

null-and-empty-string-fields

As you can see the BEARBEITET field has three different values:
ERROR
[empty string]
[NULL]

Looking at it with a Hex editor I found that a string field is treated as NULL, if it contains all spaces ($20), it is treated as an empty string, if it contains at least one NUL ($00) (the rest can also be spaces), which was the case for the second record. I can recreate the effect by changing the file with said Hex editor.

The source code in tdbf for reading a string field does the following:

  1. Get the field offset and length.
  2. Copy its content to a buffer.
  3. Starting from the end, decrement the length as long as length is > 0 and the lengthth character is a space
  4. If length is now 0, treat the value als NULL, if not, copy it to a string by typecasting the buffer to a PChar

So if the field’s content is:

00 00 00 00 00

it will converted to an empty string.

If the content is:

20 20 20 20 20

it will converted to NULL.

If the content is:

20 00 20 20 20

it will converted to ‘ ‘ (a string containing a single space).

I still have no idea how this file was created though.

Filter multiple criteria in Windows Explorer

 Windows, Windows 7, Windows 8.1  Comments Off on Filter multiple criteria in Windows Explorer
Nov 092016
 

Note to self: It is possible to filter on multiple criteria – e.g. extensions – in Windows Explorer by combining them with OR:

.txt OR .doc
  • The OR must be written all upper case (AND is also possible).
  • *.txt will not work
  • It will search recursively
  • A semicolon (as in file filters) does not work.

More on filtering, grouping and searching here.