Incorrect coordinates in mbtiles generated with Tippecanoe - mapbox

I generated an mbtiles file using Tippecanoe with just -zg and --drop-densest-as-needed as extra parameters. I uploaded the file to Mapbox Studio and everything works well, both in Studio and when loading the tiles through a mobile app.
I then tried my luck at self-hosting the tiles, using a very basic HTTP server in Go. Tiles were transferred from SQLite to a PostgreSQL database (the reason for this is Go + PSQL is the existing stack for the app).
For some reason the features are shifted depending on the zoom level. At level 1, data that's supposed to be in the US is in the Antarctic, at zoom level 2 it's off the coast of Chile, etc. The only one properly working is level 0 as there's only one tile.
I checked what tiles Mapbox was requesting when in San Francisco for zoom level 11: column 327, row 791. No tile exists for this row/col combination in the .mbtiles file although there's data there.
Is there additional things to be done to the mbtiles besides looking them up in the database using the z/x/y? Or maybe stuff to configure on the app side?
Server code:
row := db.QueryRow(`
SELECT tile_data FROM tiles
WHERE
zoom_level = $1
AND tile_column = $2
AND tile_row = $3
`,
z, x, y,
)
On Android:
map.addSource(
VectorSource(
"tiles",
TileSet("2.2.0", "http://my.local.server:4000/tiles/{z}/{x}/{y}.mvt?key=2448A697EACDDC41432AAD9A1833E")
)
)
I tried setting the VectorSource's center and bounds found in the mbtiles metadata but it didn't change anything.

So I looked into existing server implementations and it turns out the offset is because the mbtiles are stored in a TMS format in which the Y coordinate is flipped. So we just need to convert the Y from the XYZ format to get the proper tile:
From Mapbox's own Node implementation:
// Flip Y coordinate because MBTiles files are TMS.
y = (1 << z) - 1 - y;
1 << z is the number of rows for a given zoom level, or two to the power of z.
More info about XYZ vs TMS can also be found here.

Related

Converting a open street map database to a raster attribute table

I'm new to OSM and analyzing maps in general. My task is to convert a certain area from OSM (which is given in an XML format and can be converted to different types of databases) to a tensor where two dimensions give the area (to a certain resolution), and the third dimension is the "layers" - eventually getting layered matrices of bits for each possible attribute.
For example, if the area is 2*2 (it could be more or less depending on chosen resolution) and the possible attribute are buildings, trees, and roads, then the tensor would be of 2*2*3: 3 layers of 2*2 matrices for the attributes. Cell (i,j,k) in the tensor is 1 if and only if the attribute k exists in the location i,j on the map.
I tried looking online and using gdal, qgis and I am pretty lost.
Is such a procedure possible?
I do not have any experience with rasters, so I can only answer the first part of your quenstion.
Get an OSM-map with the following code:
library( OpenStreetMap )
LAT1=52;LAT2=54;LON1=4;LON2=6
#get an openstreetmap
map.osm <- OpenStreetMap::openmap( c( LAT1 - 1, LON1 - 1 ),
c( LAT2 + 1, LON2 + 1 ),
zoom = NULL,
type = "https://basemaps.cartocdn.com/light_nolabels/{z}/{x}/{y}.png",
mergeTiles = TRUE)
#plot with latest version of ggplot2
ggplot2::autoplot( map.osm )
You can (probably) build a raster using raster::raster( map.osm ), since raster contains a method for OpenStreetmapObjects

What projection is this geoTIF and how do I convert it?

I am working with viewing weather data online and the website I use (University of Oklahoma) provides a link to the data displayed as a geoTIF, for research purposes. This is a direct link to the geoTIF that I am requesting help with.
I am trying to use the image in a Mapbox map, but there seems to be an issue with projection. I use GDAL tools (though I am a novice) and I can't even figure out what projection it is in to start with. When I use gdalinfo, I get the following result :
Warning 1: RowsPerStrip not defined ... assuming all one strip.
Raster dataset parameters:
Projection:
RasterCount: 1
RasterSize (7000,3500)
Using driver GeoTIFF
Image Structure Metadata:
0: COMPRESSION=DEFLATE
1: INTERLEAVE=BAND
Corner Coordinates:
Upper Left (-130, 55)
Lower Left (-130, 20)
Upper Right (-60, 55)
Lower Right (-60, 20)
Center (-95, 37.5)
Coordinate System is:
Band 1 :
DataType: Float32
ColorInterpretation: Gray
Description:
Size (7000,3500)
BlockSize (7000,3500)
NoDataValue: -999
Offset: 0
Scale: 1
I have been converting other geoTIF files for Mapbox use with the following command successfully :
gdalwarp -t_srs EPSG:3857 example.tif example-projected.tif
... The above code always works except with the file I am needing help with. I am very new with GDAL and though I have been trying it is difficult for me. What am I not doing right and how would I do this the correct way?
Your geotiff has no embedded coordinate system associated although the corners are obviously geographic coordinates (unprojected). WGS84 is my guess.
My suggestion is defining a source coordinate system in your command [-s_srs srs_def] and see the result,
-s_srs EPSG:4326
If that doesn't work you'd better ask OU. They should know better.

Mapbox Terrain RGB image '

I am new to mapbox and am trying to get the Terrain rgb data. I have followed the example from the documentation here :
https://www.mapbox.com/help/access-elevation-data/
and used the following query:
api.mapbox.com/v4/mapbox.terrain-rgb/{z}/{x}/{y}.pngraw?access_token={my_access_token}
which works find for zoom levels from 0 to 5 and returns RGB tiles of elevation data from a high level.
api.mapbox.com/v4/mapbox.terrain-rgb/5/0/0.pngraw?access_token={my_access_token}
I need to get the data at a much higher zoom level than 5, but once I use a zoom level above 5 it returns 'Tile Does Not Exist'.
The documentation says that there is data up to zoom of 15. My access token works and I have tried x,y tiles of 0, 0 (which should exist at all levels). Has anyone any help or suggestions ?
Had a similar issue and in my case the mistake was assuming that given an {x}/{y} tuple the zoom {z} was free to change, but it isn't, if zoom changes xy changes too.
To get the correct tiles I used mercantile (pip install mercantile)
import mercantile
print(mercantile.tile(-71.0638031, 42.3578952, 15))
>>> Tile(x=9915, y=12120, z=15)

How to adjust rows and columns, retaining cell sizes in arcmap?

The Situation: I have a vector data map with all the countries in the World, including a unique landcode for every country. I need to convert it into a raster map, so I can eventually analyse (with Matlab) drought indicators for that country.
The Problem: The precipitation data I use has 720 columns and 360 rows. I want the raster map of world countries to be in the exact same amount of columns and rows. BUT the raster map also needs to have a cellsize of 0.5! Right now, when I use the vector to raster conversion tool, if I use cellsize = 0.5, the columns and rows become 720x287.
Question: How can I create a raster map of 720 columns and 360 rows, while setting cellsize at 0.5?
Thank you in advance!
Additional info:
Using ArcMap 10.4.1 and Matlab R2015b
Raster is .TIFF
Vector is .shp
As a fellow GIS user I would recommend posting your question under the GIS stack exchange, https://gis.stackexchange.com/
Beyond that there is some clarification needed. Are you saying that you are trying to create a multi band raster with 1 band for each column?
I solved the problem. In ArcMap, I imported the precipitation data (NetCDF). It was however flipped, but I could get it in the right orientation using the tools 'Flip' and 'Rotate'. Now this map had the right orientation and the right amount of columns and rows. Furthermore I just altered some of the default settings in environments: 1) I set the processing extent to the precipitation data. 2) I set the snap raster also to the precipitation data. 3) I set the cellsize of every output raster to 0.5. Finally, I converted the vector file of world countries to a ASCII file using the raster to ascii conversion tool. The ASCII file now has the same amount of rows and columns as the precipitation data!

How to extend an OSM or raster area in Qgis (into 'No Data' area)?

I need to do the opposite of clipping down a raster in Qgis (or Gdal, Python etc - even R if someone can provide the script).
I have a dataset from Open StreetMap (*.OSM) which includes the south west corner of a country. I have also converted this to a SHP file or Raster format).
To match with my other Maxent datasets I need to expand the OSM data south and west so I am only increasing the 'no data' areas at the left and bottom of the files.
Is there a way to do this in Qgis?
This process worked for me:
http://ssrebelious.blogspot.co.uk/2012/09/raster-extent-modification-using-qgis.html
"In QGIS you can change extent of the rasters. Lets examine one of the worst case scenarios. There are two overlapping (one band) rasters A and B. Say, we need to add A values to B values and get the final image to have extent that will contain both images."