Group Abstract Group Abstract

Message Boards Message Boards

How can I extract the height of a DEM point?

11 Replies
Posted 11 years ago

I imported that DEM file in R using the following commands:

# DEM file

# Set working directory
  setwd("C:\\users\\jbaldwin\\Downloads")

# Load library
  library(rgdal)

# Input data
  g = readGDAL("dem_3009147ne.dem")

# Get coordinates
  xy = coordinates(g)

# Show the structure of g
  str(g)

So getting the coordinates (in UTM's) can be done by running R from Mathematica. Alternatively by comparing what R gets and the elements that Mathematica imports, one can determine the UTM's of any element of the data matrix with

(* Get necessary elements of DEM file *)
elev = Import["C:\\Users\\jbaldwin\\Downloads\\dem_3009147ne.dem", 
   "Data"];
dim = Import["C:\\Users\\jbaldwin\\Downloads\\dem_3009147ne.dem", 
  "Dimensions"]
resolution = 
 Import["C:\\Users\\jbaldwin\\Downloads\\dem_3009147ne.dem", 
  "SpatialResolution"]
boundingBox = 
 Import["C:\\Users\\jbaldwin\\Downloads\\dem_3009147ne.dem", 
  "SpatialRange"]

followed by

(* Get elevation (in meters) and coordinates (in UTMs) for a \
particular element *)
i = 1291
j = 350
elev[[i, j]]
utmx = boundingBox[[2, 1]] + i resolution[[1]]
utmy = boundingBox[[1, 1]] + j resolution[[2]]

or a something like a Table that gets all of the UTM's for each row and each column. You'll be getting UTM's rather than latitudes and longitudes. Also, the above R code ends up with the elevation in feet. Either there's some flag in the file that says it's in feet and R keeps it in feet or Mathematica always converts to meters for this particular file format irrespective of that flag or certainly some other rule. I don't know.

POSTED BY: Jim Baldwin

Interpolating the data set contained in the DEM file is quite straightforward (see the earlier post above) as is getting the other data elements contained in the file. The key questions that are being raised here are first, how to get the actual latitude and longitude range from the file. The data elements do not appear to contain this. The second issue is, since the data represents a grid for a particular map projection, how to invert that to actually get the latitude the height for specific latitude and longitude points once those ranges are known.

POSTED BY: David Reiss

If you search in the Mathematica documentation for "DEM" you will see options for reading the data and metadata, which you will probably know how to interpret. In this example I am just using pixel count for the coordinates of the x and y axes, since I don't know how to convert to lat and long. Tooltip is used to display the elevation, when the cursor is put over a contour line. This is not quite what you want, but close.

file = "/Users/christopherpurcell/Desktop/dem_3009147ne.dem";
 data = Import[file, "CoordinateSystemInformation"] 
 bounds = 
 Import[file, 
  "SpatialRange"]  (* should be {lat, long} in dec degrees but must \
be something else, maybe meters *)
 data = Import[file, "Data"];
size = Import[file, "Dimensions"]  (* data array size *)
 interp = ListInterpolation[Transpose[data]];
ContourPlot[ 
 Tooltip[interp[x, y]], {x, 1, size[[2]]}, {y, 1, size[[1]]}, 
 Contours -> Range[10], PlotPoints -> 50]

The next step might be to put a ReliefPlot of the data in a Manipulate, and return the elevation value that corresponds to the cursor location. Something along these lines, where if you click on a point, you get a display of the elevation in meters. (I think).

Manipulate[ 
 ReliefPlot[ data , FrameTicks -> True, 
  PlotLabel -> ToString[Join[x, {interp[Apply[Sequence, x]]}]], 
  ColorFunction -> "LightTerrain", 
  PlotLegends -> Automatic], {{x, {1, 1}}, Locator}]

Next you would figure out the lat and long and use DataRange option to get the axes scaled correctly.

Thanks for the shout-out, Dr. Reiss.

I'm admittedly new to Mathematica and when I saw easy to use functions like Import["...*.dem"] and GeoGraphics and GeoStyling and DensityPlot, that obviously access the height information to choose colors, I thought I would be able to interrogate the same values that give colors to give me a numerical value, but I've not been able.

JAC

Do any of the WRI developers on the forum have any advice for Mr. Cavell? The main issue is, given a DEM file, read it in and generate a function height[latitude,longitude] based on an interpolation of the supplied grid data in the DEM file. The two things that need to be extracted and analyzed for this are

a) the latitude and longitude range of the DEM array data b) any transformations that need to be done on the data based on the map projection indicated in the DEM file

It would be a good addition to DEM import if such a function could be generated as one of the import Elements for DEM

POSTED BY: David Reiss

It seems I've hit a dead-end. I just wish I was able to take apart the pieces that are obviously present to enable the graphical representation, but it seems "over my head." Perhaps reverting to something other than Mathematica will provide an answer.

JAC

I wish to compliment Dr. Reiss for his help. I contacted him off-line because I had a follow-up question that involved a brand name other than Wolfram and didn't want to be impolite to our host. He has suggested a few ideas that seem to be getting me closer to a possible solution.

file = "...local folders.../dem_3009147ne.dem"
Import[file, "ReliefImage"]
interp = ListInterpolation[Transpose@data]
interp["Domain"]
{{1., 1225.}, {1., 1405.}}
DensityPlot[interp[x, y], {x, 1, 1225}, {y, 1, 1405}, PlotPoints -> 100]

He then suggested I revisit the GeoElevationData function, which I did. Providing a lat, log it returns a value for height, but I am wary of it because I haven't found a quotable statement of the source of that elevation result. One reason I was investigating the DEM was I know its pedigree and can stand behind the results.

A point on the peninsula looking crook in the river is 30°19'48.13"N lat, 91° 9'38.35"W lon (30.33003611, -91.16065278)

GeoElevationData[{30.33003611, -91.16065278}]

returns:

Quantity[55.7743, "Feet"]

which may be an OK value but I can't be sure. The grid on which it is based is pretty coarse compared to the DEM and when I checked specific sites in urban areas the discrepancies cause some concern.

JAC

Thanks for looking into this. The subject file (dem_3009147ne.dem) is attached. I have also tried to do something similar with embedded elevation models, also with no joy so I am trying with a local file. An example of a previous attempt is:

GeoGraphics[{EdgeForm[Black],GeoStyling["ReliefMap"],Polygon[=East Baton Rouge Parish]}, GeoBackground -> None]

(The = in front of "East Baton Rouge Parish" is ctrl-=.)

JAC

Attachments:

Could you attach your example file so that we have a chance to play with it and see what we can suggest?

POSTED BY: David Reiss
Reply to this discussion
Community posts can be styled and formatted using the Markdown syntax.
Reply Preview
Attachments
Remove
or Discard