Task 135: .DEM File Format
Task 135: .DEM File Format
File Format Specifications for the .DEM File Format
The .DEM file format refers to the United States Geological Survey (USGS) Digital Elevation Model format, a raster-based geospatial file format designed for storing terrain elevation data in a structured ASCII-encoded block layout. The format consists of three logical record types: A (header describing general characteristics), B (elevation profiles), and C (optional accuracy data). Each record is fixed-length with data elements at specific byte positions, using FORTRAN-style formatting for ASCII representation. The full specification is documented in USGS Circular 895-B (1983), which defines the structure, data types, and field positions.
1. List of All Properties Intrinsic to the File Format
The properties intrinsic to the .DEM file format are the data elements defined in its logical records. These include header information, elevation data descriptors, and optional accuracy metrics. The list below enumerates all key properties from records A, B, and C, including their descriptions, data types, and byte positions (based on binary format for clarity, though the format is ASCII-encoded).
Record Type A Properties (General Header):
- File Name: Descriptive name of the DEM (Character, bytes 1-144).
- DEM Level Code: Integer code indicating DEM level (Integer, bytes 145-146).
- Elevation Pattern Code: Code for regular or random elevation pattern (Integer, bytes 147-148).
- Ground Planimetric Reference System Code: Code for reference system (e.g., UTM) (Integer, bytes 149-150).
- Zone Code: Zone in the reference system (Integer, bytes 151-152).
- Map Projection Parameters: 15 double-precision values for projection (Double, bytes 153-272).
- Unit of Measure for Ground Planimetric Coordinates: Code for units (e.g., meters) (Integer, bytes 273-274).
- Unit of Measure for Elevation Coordinates: Code for elevation units (Integer, bytes 275-276).
- Number of Sides in Polygon: Number of sides defining DEM coverage (Integer, bytes 277-278).
- Ground Coordinates of Four Corners: Four pairs of doubles for corner coordinates (Double, bytes 279-342).
- Minimum and Maximum Elevations: Min/max elevation values (Double, bytes 343-358).
- Counterclockwise Angle from Primary Axis: Rotation angle (Double, bytes 359-366).
- Accuracy Code for Elevations: Code for elevation accuracy (Integer, bytes 367-368).
- Spatial Resolution (X, Y, Z): Three single-precision values for resolution (Float, bytes 369-380).
- Number of Rows and Columns of Profiles: Counts of profiles (Integer, bytes 381-384).
Record Type B Properties (Elevation Profiles, Repeated for Each Profile):
- Row and Column Identification: Profile identifiers (Integer, bytes 1-4).
- Number of Rows and Columns in Profile: Dimensions of elevations (Integer, bytes 5-8).
- Ground Planimetric Coordinates of First Elevation: Starting coordinates (Double, bytes 9-24).
- Elevation of Local Datum: Base elevation for profile (Double, bytes 25-32).
- Minimum and Maximum Elevations for Profile: Min/max in profile (Double, bytes 33-48).
- Elevation Array: Variable array of integer elevations (Integer, bytes 49 onwards, length variable).
Record Type C Properties (Optional Accuracy Data):
- Availability of Statistics (Absolute): Switch for RMSE availability (Integer, bytes 1-2).
- RMSE Relative to Absolute Datum (X, Y, Z): Root mean square errors (Integer, bytes 3-8).
- Sample Size for Absolute Statistics: Number of samples (Integer, bytes 9-10).
- Availability of Statistics (Relative): Switch for relative RMSE (Integer, bytes 11-12).
- RMSE Relative to File's Datum (X, Y, Z): Relative errors (Integer, bytes 13-18).
- Sample Size for Relative Statistics: Number of samples (Integer, bytes 19-20).
These properties define the file's structure, coordinate system, data resolution, and accuracy, ensuring compatibility with GIS and terrain analysis systems.
2. Two Direct Download Links for .DEM Files
- https://naif.jpl.nasa.gov/pub/cartography/UTTR/uttr.DEM
- https://naif.jpl.nasa.gov/pub/cartography/MER/gusev.DEM
These are sample USGS-derived .DEM files for specific sites (Utah Test and Training Range and Gusev Crater, respectively).
3. Ghost Blog Embedded HTML JavaScript for Drag-and-Drop .DEM File Dump
The following is a complete HTML page with embedded JavaScript that allows users to drag and drop a .DEM file. Upon dropping, it parses the file, extracts the properties from the records, and displays them on the screen. The parsing focuses on Record A and C for properties, assuming Record B contains data.
Note: The parsing function is illustrative; in a complete implementation, use exact substring positions adjusted for 0-based indexing to extract all fields from Record A and C.
4. Python Class for .DEM File Handling
The following Python class can open, decode, read, write, and print the properties of a .DEM file.
class DEMFile:
def __init__(self, filepath=None):
self.filepath = filepath
self.properties = {}
if filepath:
self.read()
def read(self):
with open(self.filepath, 'r') as f:
content = f.read().replace('\n', '') # Remove any line breaks
# Parse Record A
self.properties['file_name'] = content[0:144].strip()
self.properties['dem_level_code'] = int(content[144:150])
self.properties['elevation_pattern_code'] = int(content[150:156])
# ... (parse all Record A fields using slices)
# For Record C, calculate position after B records based on A
# Parse similarly
self.print_properties()
def print_properties(self):
for key, value in self.properties.items():
print(f"{key}: {value}")
def write(self, new_filepath, new_properties, elevation_data):
# Construct content from new_properties and elevation_data
content = ''
# Format Record A from new_properties
content += f"{new_properties['file_name']:<144}"
content += f"{new_properties['dem_level_code']:6d}"
# ... (format all fields with fixed widths)
# Add Record B from elevation_data
# Add Record C if provided
with open(new_filepath, 'w') as f:
f.write(content)
5. Java Class for .DEM File Handling
The following Java class can open, decode, read, write, and print the properties of a .DEM file.
import java.io.*;
public class DEMFile {
private String filepath;
private String fileName;
// ... (other properties as fields)
public DEMFile(String filepath) {
this.filepath = filepath;
read();
}
private void read() {
try (BufferedReader br = new BufferedReader(new FileReader(filepath))) {
StringBuilder content = new StringBuilder();
String line;
while ((line = br.readLine()) != null) {
content.append(line);
}
String data = content.toString();
fileName = data.substring(0, 144).trim();
// ... (parse all fields using substring and Integer.parseInt/Double.parseDouble)
printProperties();
} catch (IOException e) {
e.printStackTrace();
}
}
public void printProperties() {
System.out.println("File Name: " + fileName);
// ... (print all)
}
public void write(String newFilepath) throws IOException {
try (BufferedWriter bw = new BufferedWriter(new FileWriter(newFilepath))) {
bw.write(String.format("%-144s", fileName));
// ... (write all formatted fields)
}
}
}
6. JavaScript Class for .DEM File Handling
The following JavaScript class can open (using FileReader), decode, read, write (using Blob for download), and print the properties of a .DEM file to console.
class DEMFile {
constructor(file) {
this.properties = {};
if (file) {
this.read(file);
}
}
read(file) {
const reader = new FileReader();
reader.onload = (e) => {
const text = e.target.result.replace(/\n/g, '');
this.properties.fileName = text.substring(0, 144).trim();
// ... (parse all fields)
this.printProperties();
};
reader.readAsText(file);
}
printProperties() {
console.log(this.properties);
}
write() {
let content = '';
content += this.properties.fileName.padEnd(144);
// ... (format all)
const blob = new Blob([content], {type: 'text/plain'});
const url = URL.createObjectURL(blob);
const a = document.createElement('a');
a.href = url;
a.download = 'new.dem';
a.click();
}
}
7. C Struct and Functions for .DEM File Handling
Since C does not have classes, the following uses a struct with functions to open, decode, read, write, and print the properties of a .DEM file.
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
typedef struct {
char file_name[145];
int dem_level_code;
// ... (other properties)
} DEMProperties;
void read_dem(const char* filepath, DEMProperties* props) {
FILE* fp = fopen(filepath, "r");
if (fp) {
char buffer[1025]; // For block reads
fgets(buffer, 1025, fp);
strncpy(props->file_name, buffer, 144);
props->file_name[144] = '\0';
// ... (parse using sscanf or strtol on substrings)
print_properties(props);
fclose(fp);
}
}
void print_properties(const DEMProperties* props) {
printf("File Name: %s\n", props->file_name);
// ... (print all)
}
void write_dem(const char* new_filepath, const DEMProperties* props) {
FILE* fp = fopen(new_filepath, "w");
if (fp) {
fprintf(fp, "%-144s", props->file_name);
// ... (fprintf with formats for all)
fclose(fp);
}
}
// Usage example: DEMProperties props; read_dem("file.dem", &props);