Shapefile viewerPosted on 11 November 2012 in Techstuff
I wondered if geographic coordinate data (like country borders) was freely available somewhere. And what do you know? Our friend Google found ALOT. Cool, what now? Lets draw it on the screen!
Almost all sites i found offer the data in the form of ESRI shapefile files. These are binary files and i had never made a program to read a binary file. Luckily the format is super well described here. Its a PDF file that contains everything needed to build a reader. A shapefile consists of a main file(.shp), an index file(.shx), and a dBASE table(.dbf) but i only used the main .shp files because it contains all data needed for display.
There are many types of shapes, lines, polygons, points, etc. One shape file contains only one type of shape. For example the country data are all Polygons. River data could be a file containing PolyLines. Currently my shapefile reader only implements the Polygon shape type but is would be easy to extend it.
As said before i had never made a reader for a binary file format and i found something that is called "Endianness". Some integers in the file are saved as "Big endian" and others as "Little endian". I did not know what it meant so i had to look it up. In short the endianness indicates the byte order of a value.
Below you can find the downloadable source code and executable. In the links section you can find places to download .shp files that you can use with the executable.
- http://www.naturalearthdata.com Natural Earth. Good source for geographic data.
- http://www.diva-gis.org/gdata Here you can select a country and download its administrative areas.
- http://www.esri.com/library/whitepapers/pdfs/shapefile.pdf Technical description of the ESRI shapefile format.
- http://en.wikipedia.org/wiki/Endianness Wikipedia page about endianness.
- http://itouchmap.com/latlong.html Handy when you want to know the lat/long of point in the world. Used for debuging.