# PostGIS raster value of a lat/lon point

I have a tiff raster in my PostGIS 2.0 database. I want to get the value of the raster at a specified lat/lon point.

I have:

• postgresql-9.1
• postgis-2.0-svn with raster support
• a raster imported into PostGIS its SRID is 3035

You can transform the lat/lon point to raster CRS on the fly using ST_Transform(). Combined, the query looks like this:

ST_Value(your_raster, ST_Transform(ST_SetSRID(ST_MakePoint(lon,lat),4326),3035))

The function ST_value:

http://postgis.net/docs/RT_ST_Value.html

returns a raster value given a point in either x,y or lat,lon.

While ST_Value works, you still need to spatially join to the point as well:

SELECT ST_Value(your_raster.rast, ST_Transform(ST_SetSRID(ST_MakePoint(lon,lat),4326),3035)) FROM your_raster WHERE ST_Intersects(your_raster.rast, ST_SetSRID(ST_MakePoint(lon,lat),4326),3035))

Or with a table:

SELECT ST_Value(your_raster.rast, ST_Transform(ST_SetSRID(ST_MakePoint(your_table.lon,your_table.lat),4326),3035)) FROM your_raster JOIN your_table ON ST_Intersects(your_raster.rast, ST_SetSRID(ST_MakePoint(your_table.lon,your_table.lat),4326),3035))

## Mathematics for a New Grid System

Euclidian or point-based geometric systems are based on the idea of a point. A point is a mathematical construct that has no equivalent on the planet. For example, a point occupies no space, whereas all real objects do occupy space at some level.

[image for point-based system]

However, digital systems are based on the idea of a discrete cell containing a quantized value, usually an integer. A cell does occupy space. Moreover, because the cell has area and because it can be subdivided into smaller cells, it can absorb a range of errors around a point in a way that a point-based system cannot.

[image for cell-based system]

Because cells more fully represent geography, mass, and quantity, cell-based systems are more useful for geodata.

The process of transforming point-based data (or analog data) into mathematical cells (or digital data) is called gridding or sampling. It is similar to the way digital recordings (CDs) are created from old recordings: by sampling it at very fine resolutions, transforming it to numbers, and then performing analysis and error correction on the digitized data.

## PostGIS raster value of a lat/lon point - Geographic Information Systems

In examples provided in prior chapters, geodata was in the form of individual variables. Mapping Toolbox™ software also provides an easy means of displaying, extracting, and manipulating collections of vector map features organized in geographic data structures .

A geographic data structure is a MATLAB ® structure array that has one element per geographic feature. Each feature is represented by coordinates and attributes. A geographic data structure that holds geographic coordinates (latitude and longitude) is called a geostruct, and one that holds map coordinates (projected x and y ) is called a mapstruct. Geographic data structures hold only vector features and cannot be used to hold raster data (regular or geolocated data grids or images).

### Shapefiles

Geographic data structures most frequently originate when vector geodata is imported from a shapefile. The Environmental Systems Research Institute designed the shapefile format for vector geodata. Shapefiles encode coordinates for points, multipoints, lines, or polygons, along with non-geometrical attributes.

A shapefile stores attributes and coordinates in separate files it consists of a main file, an index file, and an xBASE file. All three files have the same base name and are distinguished by the extensions .shp , .shx , and .dbf , respectively. (For example, given the base name 'concord_roads' the shapefile file names would be 'concord_roads.shp' , 'concord_roads.shx' , and 'concord_roads.dbf' ).

### The Contents of Geographic Data Structures

The shaperead function reads vector features and attributes from a shapefile and returns a geographic data structure array. The shaperead function determines the names of the attribute fields at run-time from the shapefile xBASE table or from optional, user-specified parameters. If a shapefile attribute name cannot be directly used as a field name, shaperead assigns the field an appropriately modified name, usually by substituting underscores for spaces.

Fields in a Geographic Data Structure

One of the following shape types: 'Point' , 'MultiPoint' , 'Line' , or 'Polygon' .

Specifies the minimum and maximum feature coordinate values in each dimension in the following form:

[ min ( X ) min ( Y ) max ( X ) max ( Y ) ]

Omitted for shape type 'Point' .

1-by-N array of class double

character vector or scalar number

Attribute name, type, and value.

Optional. There are usually multiple attributes.

The shaperead function does not support any 3-D or "measured" shape types: 'PointZ' , 'PointM' , 'MultipointZ' , 'MultipointM' , 'PolyLineZ' , 'PolyLineM' , 'PolygonZ' , 'PolylineM' , or 'Multipatch' . Also, although 'Null Shape' features can be present in a 'Point' , 'Multipoint' , 'PolyLine' , or 'Polygon' shapefile, they are ignored.

#### PolyLine and Polygon Shapes

In geographic data structures with Line or Polygon geometries, individual features can have multiple parts—disconnected line segments and polygon rings. The parts can include counterclockwise inner rings that outline "holes." For an illustration of this, see Create and Display Polygons. Each disconnected part is separated from the next by a NaN within the X and Y (or Lat and Lon ) vectors. You can use the isShapeMultipart function to determine if a feature has NaN-separated parts.

Each multipoint or NaN-separated multipart line or polygon entity constitutes a single feature and thus has one character vector or scalar double value per attribute field. It is not possible to assign distinct attributes to the different parts of such a feature any character vector or numeric attribute imported with (or subsequently added to) the geostruct or mapstruct applies to all the feature's parts in combination.

#### Mapstructs and Geostructs

By default, shaperead returns a mapstruct containing X and Y fields. This is appropriate if the data set coordinates are already projected (in a map coordinate system). Otherwise, if the data set coordinates are unprojected (in a geographic coordinate system), use the parameter-value pair 'UseGeoCoords',true to make shaperead return a geostruct having Lon and Lat fields.

Coordinate Types. If you do not know whether a shapefile uses geographic coordinates or map coordinates, here are some things you can try:

If the shapefile includes a projection file ( .prj ), use shapeinfo to get information about the coordinate reference system. If the CoordinateReferenceSystem field of the returned structure is a projcrs object, you have map coordinates. If the field is a geocrs object, you have geographic coordinates.

If the shapefile does not include a projection file, use shapeinfo to obtain the BoundingBox . By looking at the ranges of coordinates, you may be able to tell what kind of coordinates you have.

The geoshow function displays geographic features stored in geostructs, and the mapshow function displays geographic features stored in mapstructs. If you try to display a mapstruct with geoshow , the function issues a warning and calls mapshow . If you try to display a geostruct with mapshow , the function projects the coordinates with a Plate Carree projection and issues a warning.

### Examining a Geographic Data Structure

Here is an example of an unfiltered mapstruct returned by shaperead :

The output appears as follows:

The shapefile contains 609 features. In addition to the Geometry , BoundingBox , and coordinate fields ( X and Y ), there are five attribute fields: STREETNAME , RT_NUMBER , CLASS , ADMIN_TYPE , and LENGTH .

The output appears as follows:

This mapstruct contains 'Line' features. The tenth line has nine vertices. The values of the first two attributes are character vectors. The second happens to be an empty character vector. The final three attributes are numeric. Across the elements of S , X and Y can have various lengths, but STREETNAME and RT_NUMBER must always contain character vectors, and CLASS , ADMIN_TYPE and LENGTH must always contain scalar doubles.

In this example, shaperead returns an unfiltered mapstruct. If you want to filter out some attributes, see Select Shapefile Data to Read for more information.

### How to Construct Geographic Data Structures

Functions such as shaperead or gshhs return geostructs when importing vector geodata. However, you might want to create geostructs or mapstructs yourself in some circumstances. For example, you might import vector geodata that is not stored in a shapefile (for example, from a MAT-file, from an Microsoft ® Excel ® spreadsheet, or by reading in a delimited text file). You also might compute vector geodata and attributes by calling various MATLAB or Mapping Toolbox functions. In both cases, the coordinates and other data are typically vectors or matrices in the workspace. Packaging variables into a geostruct or mapstruct can make mapping and exporting them easier, because geographic data structures provide several advantages over coordinate arrays:

All associated geodata variables are packaged in one container, a structure array.

The structure is self-documenting through its field names.

You can vary map symbology for points, lines, and polygons according to their attribute values by constructing a symbolspec for displaying the geostruct or mapstruct.

A one-to-one correspondence exists between structure elements and geographic features, which extends to the children of hggroup objects constructed by mapshow and geoshow .

Achieving these benefits is not difficult. Use the following example as a guide to packaging vector geodata you import or create into geographic data structures.

#### Making Point and Line Geostructs

The following example first creates a point geostruct containing three cities on different continents and plots it with geoshow . Then it creates a line geostruct containing data for great circle navigational tracks connecting these cities. Finally, it plots these lines using a symbolspec.

Begin with a small set of point data, approximate latitudes and longitudes for three cities on three continents:

Build a point geostruct it needs to have the following required fields:

Geometry (in this case 'Point' )

Lat (for points, this is a scalar double)

Lon (for points, this is a scalar double)

Display the geostruct on a Mercator projection of the Earth's land masses stored in the landareas.shp shapefile, setting map limits to exclude polar regions:

Next, build a Line geostruct to package great circle navigational tracks between the three cities:

Compute lengths of the great circle tracks:

Map the three tracks in the line geostruct:

You can save the geostructs you just created as shapefiles by calling shapewrite with a file name of your choice, for example:

#### Making Polygon Geostructs

Creating a geostruct or mapstruct for polygon data is similar to building one for point or line data. However, if your polygons include multiple, NaN-separated parts, recall that they can have only one value per attribute, not one value per part. Each attribute you place in a structure element for such a polygon pertains to all its parts. This means that if you define a group of islands, for example with a single NaN-separated list for each coordinate, all attributes for that element describe the islands as a group, not particular islands. If you want to associate attributes with a particular island, you must provide a distinct structure element for that island.

Be aware that the ordering of polygon vertices matters. When you map polygon data, the direction in which polygons are traversed has significance for how they are rendered by functions such as geoshow , mapshow , and mapview . Proper directionality is particularly important if polygons contain holes. The Mapping Toolbox convention encodes the coordinates of outer rings (e.g., continent and island outlines) in clockwise order counterclockwise ordering is used for inner rings (e.g., lakes and inland seas). Within the coordinate array, each ring is separated from the one preceding it by a NaN.

When plotted by mapshow or geoshow , clockwise rings are filled. Counterclockwise rings are unfilled any underlying symbology shows through such holes. To ensure that outer and inner rings are correctly coded according to the above convention, you can invoke the following functions:

ispolycw — True if vertices of polygonal contour are clockwise ordered

poly2cw — Convert polygonal contour to clockwise ordering

poly2ccw — Convert polygonal contour to counterclockwise ordering

poly2fv — Convert polygonal region to face-vertex form for use with patch in order to properly render polygons containing holes

Three of these functions check or change the ordering of vertices that define a polygon, and the fourth one converts polygons with holes to a completely different representation.

For an example of working with polygon geostructs, see Converting Coastline Data (GSHHG) to Shapefile Format.

### Mapping Toolbox Version 1 Display Structures

Prior to Version 2, when geostructs and mapstructs were introduced, a different data structure was employed when importing geodata from certain external formats to encapsulate it for map display functions. These display structures accommodated both raster and vector map data and other kinds of objects, but lacked the generality of current geostructs and mapstructs for representing vector features and are being phased out of the toolbox. However, you can convert display structures that contain vector geodata to geostruct form using updategeostruct . For more information about Version 1 display structures and their usage, see Version 1 Display Structures in the reference page for displaym . Additional information is located in reference pages for updategeostruct , extractm , and mlayers .