More

Getting time-zones from latitude and longitude in raster using R?

Getting time-zones from latitude and longitude in raster using R?


I have a raster with longitudes and latitudes for the US with meteorological data (for about 100k grid points). I would like to get the time zone for each point in order to transform the time to local time. Here is how my raster look like:

> raster class : SpatialPointsDataFrame features : 95025 extent : -179.9979, 179.9918, 1, 85.33336 (xmin, xmax, ymin, ymax) coord. ref. : +proj=longlat +ellps=WGS84 +datum=WGS84 +no_defs +towgs84=0,0,0 variables : 1 names : narr.a_221_20141002_1200_000.sub min values : 0 max values : 100

This problem is somewhat similar to https://stackoverflow.com/questions/23414340/convert-to-local-time-zone-using-latitude-and-longitude, except that I have lots of points. The suggested solution there does not work as the Google API accepts only limited number of requests per day and I do not think that the brute force solution would be the most efficient.

I am an absolute beginner in geographical data, but familiar with R, so I need a solution which works in R.


As the number of points are way to much to request the timezone by lon/lat for each, the solution of @MickyT worked:

tz <- shapefile("us/tz_us.shp") tz_polygon <- SpatialPolygons([email protected], [email protected]) pts_over_tz <- over(raster, tz)

(Also note that my "raster" is actually aSpatialPointsDataFrameobject, created byrasterToPoints(r, spatial = TRUE)whereris indeed aRasterLayerobject.)


For future reference, this can be easily achieved withtz_lookup()from the lutz package. Using US cities as input point data:

library(USAboundaries) library(lutz) cts = us_cities() tzs = tz_lookup(cts) table(tzs) America/Anchorage America/Boise America/Chicago 12 47 2658 America/Denver America/Detroit America/Hermosillo 313 222 1 America/Indiana/Indianapolis America/Indiana/Knox America/Indiana/Tell_City 114 1 1 America/Indiana/Vincennes America/Juneau America/Kentucky/Louisville 6 1 19 America/Kentucky/Monticello America/Los_Angeles America/Matamoros 1 850 3 America/Menominee America/Moncton America/New_York 4 1 2507 America/Nome America/North_Dakota/Beulah America/North_Dakota/New_Salem 3 1 1 America/Phoenix America/Sitka America/Toronto 76 3 2 America/Vancouver America/Winnipeg Pacific/Honolulu 1 1 83

The sunrise equation is what you're looking for. This gives the number of hours of daylight as a function of your latitude and the Sun's declination the declination is itself a function of the day of the year.

From that one could figure out the time of sunrise (using the longitude and time zone for correcting for the position in the time zone), assuming that noon is exactly halfway between sunrise and sunset.

But ignoring time zones and daylight savings time, the time halfway between sunrise and sunset is not noon! On average it's noon, but sometimes the sun runs slow and sometimes it runs fast. The correction for this is called the "equation of time" the Wikipedia article gives an approximate formula good to within 45 seconds.


1 Answer 1

This is possible using the extract function from raster. Or also possible with the over function from the sp package. The approach I have taken below is done using the extract function:

I first used your data provided above to make a data frame, and then got the Brazil shapefile, and used it to extract the city name from the coordinates provided. Here is my code with comments:

This is what we get at the end:


Getting time-zones from latitude and longitude in raster using R? - Geographic Information Systems

To project or unproject regularly-spaced raster data that is associated with a geographic or map reference object, you must first create a coordinate grid that matches the size of the raster. Use different grid creation functions depending on which way you are projecting. When you project latitude-longitude coordinates to x - y coordinates, create a grid using the geographicGrid function. When you unproject x - y coordinates to latitude-longitude coordinates, create a grid using the worldGrid function.

After transforming the raster data, you can display it on a map using visualization functions such as mapshow and geoshow . Use mapshow for projected x - y coordinates and geoshow for unprojected latitude-longitude coordinates.

Project Raster Data

To project data that is associated with a geographic raster reference object, first create a grid of latitude-longitude coordinates for each point in the raster. Then, project the geographic coordinates to x - y map coordinates.

For example, import elevation raster data as an array and a geographic cells reference object. Get the latitude-longitude coordinates for each point in the raster by using the geographicGrid function.

Now that you have your grid, select a map projection to use when projecting the coordinates. For this example, create a projcrs object for UTM zone 13 in the northern hemisphere. Then, project the latitude-longitude coordinates to x - y coordinates.

Display the projected raster as a surface by calling mapshow and specifying the x - y coordinates and elevation array. Add axis labels and apply a colormap appropriate for elevation data.

If the geographic CRS of the latitude-longitude coordinates does not match the geographic CRS of the projected CRS, then the projected coordinates may be inaccurate. You can find the geographic CRS of a projcrs object or a geographic raster reference object by querying their GeographicCRS properties.


Getting time-zones from latitude and longitude in raster using R? - Geographic Information Systems

To project or unproject regularly-spaced raster data that is associated with a geographic or map reference object, you must first create a coordinate grid that matches the size of the raster. Use different grid creation functions depending on which way you are projecting. When you project latitude-longitude coordinates to x - y coordinates, create a grid using the geographicGrid function. When you unproject x - y coordinates to latitude-longitude coordinates, create a grid using the worldGrid function.

After transforming the raster data, you can display it on a map using visualization functions such as mapshow and geoshow . Use mapshow for projected x - y coordinates and geoshow for unprojected latitude-longitude coordinates.

Project Raster Data

To project data that is associated with a geographic raster reference object, first create a grid of latitude-longitude coordinates for each point in the raster. Then, project the geographic coordinates to x - y map coordinates.

For example, import elevation raster data as an array and a geographic cells reference object. Get the latitude-longitude coordinates for each point in the raster by using the geographicGrid function.

Now that you have your grid, select a map projection to use when projecting the coordinates. For this example, create a projcrs object for UTM zone 13 in the northern hemisphere. Then, project the latitude-longitude coordinates to x - y coordinates.

Display the projected raster as a surface by calling mapshow and specifying the x - y coordinates and elevation array. Add axis labels and apply a colormap appropriate for elevation data.

If the geographic CRS of the latitude-longitude coordinates does not match the geographic CRS of the projected CRS, then the projected coordinates may be inaccurate. You can find the geographic CRS of a projcrs object or a geographic raster reference object by querying their GeographicCRS properties.


Creation

You can use these functions to create GeographicCellsReference objects:

readgeoraster — Import a raster data file as an array and a reference object.

wmsread — Import a raster map from a Web Map Service (WMS) server as an array and a reference object.

georefcells — Create a geographic raster reference object by specifying the latitude and longitude limits of the raster and either the size of the raster or the extent of the cells.

georasterref — Convert a world file to a geographic raster reference object.

refmatToGeoRasterReference — Convert a referencing matrix to a geographic raster reference object.

refvecToGeoRasterReference — Convert a referencing vector to a geographic raster reference object.


Creation

You can use these functions to create GeographicPostingsReference objects:

readgeoraster — Import a raster data file as an array and a reference object.

georefpostings — Create a geographic raster reference object by specifying the latitude and longitude limits of the raster and either the size of the raster or the spacing of the postings.

georasterref — Convert a world file to a geographic raster reference object.

refmatToGeoRasterReference — Convert a referencing matrix to a geographic raster reference object.


Maptrims will be removed

Not recommended starting in R2020b

Some functions that accept referencing vectors or referencing matrices as input will be removed, including the maptrims function. Use a geographic reference object and the geocrop function instead. Reference objects have several advantages over referencing vectors and matrices.

Unlike referencing vectors and matrices, reference objects have properties that document the size of the associated raster, its geographic limits, and the direction of its rows and columns. For more information about reference object properties, see the GeographicCellsReference and GeographicPostingsReference objects.

Manipulate the limits of geographic rasters associated with reference objects using the geocrop function.

Manipulate the size and resolution of geographic rasters associated with reference objects using the georesize function.

To update your code, first create a reference object for either a raster of cells using the georefcells function or a raster of regularly posted samples using the georefpostings function. Alternatively, convert from a referencing vector or a referencing matrix to a reference object using the refvecToGeoRasterReference or refmatToGeoRasterReference function, respectively.

Then, replace instances of the maptrims function with the geocrop function using these patterns.


Creating Map Displays with Data in Projected Coordinate Reference System

This example illustrates how to import and display geographic data that contain coordinates in a projected coordinate reference system.

In particular, this example illustrates how to

Import specific raster and vector data sets

Create map displays for visualizing the data

Display multiple data sets in a map display

Display multiple data sets with coordinates in geographic and projected coordinate reference systems in a single map display

Example 1: Import Raster Data in Projected Coordinate Reference System

Geographic raster data that contains coordinates in a projected coordinate reference system can be stored in a variety of different formats, including standard file formats such as GeoTIFF, Spatial Data Transfer Standard (SDTS), NetCDF, HDF4, or HDF5. This example illustrates importing data from a GeoTIFF file. The data in the file contains coordinates in the projected map coordinate reference system Massachusetts State Plane Mainland Zone coordinate system .

The coordinates of the image in the GeoTIFF file, boston.tif , are in a projected coordinate reference system. You can determine that by using the geotiffinfo function and examine the PCS and Projection field values.

The length unit of the coordinates are defined by the UOMLength field in the info structure.

To import the image and the spatial referencing object, use readgeoraster .

Example 2: Display Raster Data in Projected Coordinate Reference System

You can display the image on a regular MATLAB axes using mapshow , which displays the image and sets the axes limits to the limits defined by the referencing object, R . The coordinates, as mentioned above, are in US survey foot and are relative to an origin to the southwest of the map, which is why the numbers are large. The coordinates are always positive within the zone.

Example 3: Import Vector Data in Projected Coordinate Reference System

Geographic vector data that contains coordinates in a projected coordinate reference system can be stored in shapefiles. This example illustrates how to import vector data in a projected coordinate reference system from the shapefile, boston_roads.shp .

Import vector line data from the boston_roads.shp file.

To get information about the projected coordinate reference system, first return information about the shapefile as a structure. Then, query the CoordinateReferenceSystem field.

Example 4: Display Vector and Raster Data in Projected Coordinate Reference System

The vector and raster data in this example are in the same projected coordinate reference system. However, the vector data is in length units of meter, while the raster data is in length unit of survey foot. Convert the raster data to length units of meter and display the data on the same axes.

Convert the coordinates of the raster image from units of US survey foot to meter.

Display the raster image and vector data using mapshow .

Example 5: Display Data in both Geographic and Projected Coordinate Reference Systems

You may have geographic data whose coordinates are in latitude and longitude and other data whose coordinates are in a projected coordinate reference system. You can display these data sets in the same map display. This example illustrates how to display data in a geographic coordinate reference system (latitude and longitude) with data in a projected map coordinate reference system (Massachusetts State Plane Mainland Zone coordinate system).

Read a raster image with a worldfile whose coordinates are in latitude and longitude. Use imread to read the image and worldfileread to read the worldfile and construct a spatial referencing object.

To display the overview image and the GeoTIFF image in the same map display, you need to create a map display using a Mapping Toolbox™ projection structure containing the projection information for the data in the projected coordinate reference system, Massachusetts State Plane Mainland Zone coordinate system . To make a map display in this system, you can use the projection information contained in the GeoTIFF file. Use the geotiff2mstruct function to construct a Mapping Toolbox™ projection structure, from the contents of the GeoTIFF information structure. The geotiff2mstruct function returns a projection in units of meters. Use the projection structure to define the projection parameters for the map display.

Use the latitude and longitude limits of the Boston overview image.

Create a map display by using the projection information stored in the map projection structure and set the map latitude and longitude limits. Display the geographic data in the map axes. geoshow projects the latitude and longitude coordinates.

Since the coordinates of the GeoTIFF image are in a projected coordinate reference system, use mapshow to overlay the more detailed Boston image onto the display. Plot the boundaries of the Boston image in red.

Zoom to the geographic region of the GeoTIFF image by setting the axes limits to the limits of the Boston image and add a small buffer. Note that the buffer size ( delta ) is expressed in meters.

You can overlay the road vectors onto the map display. Use a symbol specification to give each class of road its own color.

You can also overlay data from a GPS stored in a GPX file. Import point geographic vector data from the boston_placenames.gpx file included with the Mapping Toolbox™ software. The file contains latitude and longitude coordinates of geographic point features in part of Boston, Massachusetts, USA. Use gpxread to read the GPX file and return a geopoint vector .

Overlay the placenames onto the map and increase the marker size, change the markers to circles and set their edge and face colors to yellow.

Data Set Information

The files boston.tif and boston_ovr.jpg include materials copyrighted by GeoEye, all rights reserved. GeoEye was merged into the DigitalGlobe corporation on January 29th, 2013. For more information about the data sets, use the commands type boston.txt and type boston_ovr.txt .

The files boston_roads.shp and boston_placenames.gpx are from the Bureau of Geographic Information (MassGIS), Commonwealth of Massachusetts, Executive Office of Technology and Security Services. For more information about the data sets, use the commands type boston_roads.txt and type boston_placenames_gpx.txt .


Maptrims will be removed

Not recommended starting in R2020b

Some functions that accept referencing vectors or referencing matrices as input will be removed, including the maptrims function. Use a geographic reference object and the geocrop function instead. Reference objects have several advantages over referencing vectors and matrices.

Unlike referencing vectors and matrices, reference objects have properties that document the size of the associated raster, its geographic limits, and the direction of its rows and columns. For more information about reference object properties, see the GeographicCellsReference and GeographicPostingsReference objects.

Manipulate the limits of geographic rasters associated with reference objects using the geocrop function.

Manipulate the size and resolution of geographic rasters associated with reference objects using the georesize function.

To update your code, first create a reference object for either a raster of cells using the georefcells function or a raster of regularly posted samples using the georefpostings function. Alternatively, convert from a referencing vector or a referencing matrix to a reference object using the refvecToGeoRasterReference or refmatToGeoRasterReference function, respectively.

Then, replace instances of the maptrims function with the geocrop function using these patterns.