Shapefile Format: Technical Architecture and File Structure

Shapefile Format: Technical Architecture and File Structure

The Shapefile is a widely used vector data format for Geographic Information System (GIS) software. Developed by ESRI, it stores geometric location and attribute information. While often referred to as a single file, a complete shapefile consists of several mandatory and optional files that work together to define spatial features.

It is important to distinguish the ESRI Shapefile from the AutoCAD shape font source format; although both share the .shp extension, they are entirely different formats.

Key Facts

  • .shp: The mandatory main file containing geometry data.
  • .shx: The mandatory index file used for seeking records.
  • .dbf: The mandatory attribute file using the dBase IV format.
  • .sbn: An optional binary spatial index used primarily by ESRI software.
  • Coordinate Systems: Uses a Cartesian system (X Y / Easting Northing) or Geographic system (longitude latitude).
  • Dimensionality: Supports 2D, 3D (Z for elevation), and 4D (M for linear referencing or time).

The Main Geometry File (.shp)

The .shp file stores the actual geometry of features as a set of vector coordinates. The file structure begins with a fixed-length header of 100 bytes, followed by one or more variable-length records. Each record consists of a record header and the record contents.

The 100-byte header contains 17 fields: nine 4-byte signed integers (int32) and eight 8-byte signed floating-point numbers (doubles). These fields define the file code, length, version, shape type, and the Minimum Bounding Rectangle (MBR)—the smallest rectangle that encompasses all shapes in the dataset.

[ไม่มีภาพประกอบ]

Coordinate Dimensions

Beyond standard 2D coordinates, the format supports additional dimensions:

  • Z-dimension: Stores elevation for 3D space, enabling 3D visualization and analysis.
  • M-dimension: A user-defined measure used for functions like linear referencing or recording the relative time of a feature in 4D space.

Record Structure and Shape Types

Each variable-length record is prefixed by an 8-byte header containing the record number (1-based) and the record length. The subsequent record content depends on the Shape Type. Common types include Points, Polylines, Polygons, and MultiPoints, with corresponding "Z" and "M" variants for higher-dimensional data.

Value Shape Type Key Fields
0 Null shape None
1 Point X, Y
3 Polyline MBR, Parts, Points
5 Polygon MBR, Parts, Points
8 MultiPoint MBR, Points
11 PointZ X, Y, Z (Optional M)
31 MultiPatch MBR, Parts, Part types, Points, Z range, Z array

Supporting File Formats

Shape Index Format (.shx)

The .shx file provides a positional index of the feature geometry. It shares the same 100-byte header as the .shp file but follows it with fixed-length 8-byte records containing the record offset and length. This allows software to seek forwards or backwards through the main .shp file efficiently.

While a .shx file can be regenerated from a .shp file, doing so is generally considered a repair of a corrupt dataset, as the index is intended to be present by default.

Attribute Format (.dbf)

The .dbf file stores the attributes associated with each geometric shape. It utilizes the dBase IV format, a public standard implemented by various xBase clones. Because attribute names and values are not standardized, they vary depending on the data source.

Spatial Index Format (.sbn)

The .sbn file is a binary spatial index used exclusively by ESRI software. Although the format is not officially documented by ESRI, it has been reverse-engineered by the open-source community. It is not strictly necessary for parsing spatial data, as the .shp file contains all essential geometry information.

Frequently Asked Questions

What is the difference between .shp and .shx?

The .shp file contains the actual geometry and coordinate data, while the .shx file is an index that stores the location (offset) and length of those records, allowing for faster data retrieval.

Can a shapefile store 3D data?

Yes, shapefiles support Z-coordinates for elevation. Specific shape types, such as PointZ, PolylineZ, and PolygonZ, are designed to handle 3D spatial data.

What is the purpose of the M dimension?

The M dimension is a user-defined measure. It is commonly used for linear referencing or to store a time component, effectively allowing the data to represent 4D space.

Is the .sbn file required to open a shapefile?

No, the .sbn file is not strictly necessary. It is a spatial index used primarily by ESRI software to optimize performance, but the geometry can be parsed using only the .shp file.

What format is used for the attribute data in a shapefile?

Attribute data is stored in a .dbf file using the dBase IV format, which is also compatible with various xBase implementations.