How to avoid compression when converting GeoTIFF to MBTiles? - gis

I have been trying to convert a set of GeoTIFF files into MBTiles using gdal_translate (GDAL 3.0.4). My command looks as follows:
gdal_translate -of MBTiles -ot Byte -strict -scale 0 255 bogota.tif bogota.mbtiles
The GeoTIFF image is successfully converted to MBTiles, and I am able to render it using QGIS. However, it appears that the result is somewhat compressed, or that the new image has lost some resolution. I have been experimenting with the -outsize option and trying to force it to 100% of the original size of the image, but with no success.
Is there a way to make sure that the result maintains the full resolution in the output?
Here are some screenshots to compare the results:
Before
After
Note: GeoTIFF image is taken from the following link:
https://download.osgeo.org/geotiff/samples/made_up/bogota.tif

Related

Netlogo GIS extension: raster won't patch NetLogo world

I'm trying to import a raster grid to NetLogo but am encountering many issues. My raster file is only 57x41 pixels (I want each pixel here to represent a patch) and the world envelope is [-382875 -381135 700185 701445]. I am also trying to match my raster-dataset value to the patch variable fuel-code in a .csv file. However when I run the code (below) I encounter errors. I'm not using a set coordinate projection in netlogo since my original raster is not in an acceptable projection type for NetLogo (I removed the .prj file associated with the raster when importing the .asc file). Below is my code (with included error messages to the code I tried to edit):
extensions [ csv table gis]
globals [ fuel-type-40 fuel-code setrial1]
to dictionary-file ;put in the setup procedure
ca
;load the ascii file
set setrial1 gis:load-dataset "setrial_ascii.asc"
;match dimensions of raster to the dimensions of the Netlogo world
;I've tried each of the below codes independently, not together
resize-world 0 gis:width-of setrial1 0 gis:height-of setrial1 ;ERROR: Java Heap space error
gis:set-world-envelope gis:envelope-of setrial1 ;ERROR: can't modify a patch's coordinates
;below is visuals of width and height of setrial1
print gis:height-of setrial1 ;41
print gis:width-of setrial1 ;57
print envelope-of setrial1 ;[-382875 -381135 700185 701445]
; Load the csv
set fuel-type-40 but-first csv:from-file "fuel-type-40.csv"
;print fuel-type-40
; Pull first value (Fuel-code)
set fuel-code map first fuel-type-40
;print fuel-code
ask patches [
; Randomly set patch 'land cover' for this example. change for raster
gis:apply-raster setrial1 fuel-code
]
end
You might report which errors you are having. Remember to always read the stackoverflow guide to asking questions.
I'd suggest you to focus on using a rasterfile with all your data on it (ESRI files have a .dbf file which supports data), and thus avoid using both extensions. By having a raster file with a defined resolution (e.g. 100 x 100 m), the resize-world function should work smoothly. Try following my answer to this question.

How to Convert Open Image Dataset to LMDB [duplicate]

I am relatively new to machine learning/python/ubuntu.
I have a set of images in .jpg format where half contain a feature I want caffe to learn and half don't. I'm having trouble in finding a way to convert them to the required lmdb format.
I have the necessary text input files.
My question is can anyone provide a step by step guide on how to use convert_imageset.cpp in the ubuntu terminal?
Thanks
A quick guide to Caffe's convert_imageset
Build
First thing you must do is build caffe and caffe's tools (convert_imageset is one of these tools).
After installing caffe and makeing it make sure you ran make tools as well.
Verify that a binary file convert_imageset is created in $CAFFE_ROOT/build/tools.
Prepare your data
Images: put all images in a folder (I'll call it here /path/to/jpegs/).
Labels: create a text file (e.g., /path/to/labels/train.txt) with a line per input image . For example:
img_0000.jpeg 1
img_0001.jpeg 0
img_0002.jpeg 0
In this example the first image is labeled 1 while the other two are labeled 0.
Convert the dataset
Run the binary in shell
~$ GLOG_logtostderr=1 $CAFFE_ROOT/build/tools/convert_imageset \
--resize_height=200 --resize_width=200 --shuffle \
/path/to/jpegs/ \
/path/to/labels/train.txt \
/path/to/lmdb/train_lmdb
Command line explained:
GLOG_logtostderr flag is set to 1 before calling convert_imageset indicates the logging mechanism to redirect log messages to stderr.
--resize_height and --resize_width resize all input images to same size 200x200.
--shuffle randomly change the order of images and does not preserve the order in the /path/to/labels/train.txt file.
Following are the path to the images folder, the labels text file and the output name. Note that the output name should not exist prior to calling convert_imageset otherwise you'll get a scary error message.
Other flags that might be useful:
--backend - allows you to choose between an lmdb dataset or levelDB.
--gray - convert all images to gray scale.
--encoded and --encoded_type - keep image data in encoded (jpg/png) compressed form in the database.
--help - shows some help, see all relevant flags under Flags from tools/convert_imageset.cpp
You can check out $CAFFE_ROOT/examples/imagenet/convert_imagenet.sh
for an example how to use convert_imageset.

MATLAB: How to read blob data from MySQL, and save as image?

I have a bunch of images stored as blob data in database, now I would like to read(retrieve) these images from mysql using MATLAB, then do further image processing. However, the problem is: retrieved data is a N*1 unit8 vector, which can not be used for image feature (such as SIFT) extraction implementation. The following is my code:
conn = database.ODBCConnection('test','username','password');
curs = exec(conn,'select image from roomimage'); % image is the column of saved blob data, and roomimage is the table in database
curs = fetch(curs);
img=curs.Data{1,1}; % as and example, read the first image
Then I get class(img)=unit8, size is 111365*1
Then if I write and save as image file:
imwrite(img,'test.jpg')
imwrite(img,'test.png') % alternatively, I also tried to save it as .png file
Here comes the problem: the saved file cannot be opened! :(
Could someone show me, when using MATLAB, how to read blob data from mysql, save it as .jpg file, which can be opened as an image? Thank you very much!!!
#
Add: if I export and save image from mysql beforehand, then load image from local file to MATLAB, I get something like:683*1024*3 unit8 data, which is NOT like the N*1 vector read from mysql...

How to open gnuplots in full screen and a particular size?

I am plotting graphs in gnuplot and would like to open them in full screen and a particular size.
Previously, I have been outputting graphs in multiplot mode and updating them using reread; so, when I maximise it manually, the plots fill the screen after a few iterations. Now, I also want to save the output as a file. When I open that file, it is in the same small size as the original multiplot output. However, when I maximise it, the plots don't increase in size to fill the screen. I have 2 questions:
How can I open the multiplot file in full screen?
How can I make the output file a particular size?
Here is my current gnuplot code (in a file called gnuplotCode):
set terminal pngcairo dashed enhanced
set output 'foo.png'
set multiplot layout 3, 3
plot for [iter=1:9] path/to/file using 1:(column(iter)) notitle
unset multiplot
unset output
pause 10
reread
I have tried to type the following:
gnuplot -geometry -3360-1050 gnuplotCode # where my screen size is 3360x1050
and:
resolution=$(xrandr | grep '*') && resolution=${resolution% *}
gnuplot -geometry $resolution gnuplotCode
but neither approach works. Please can you tell me how to open gnuplots in full screen and a particular size? Thank you.
You must distinguish between pixel-based terminals (pngcairo, png, canvas (...) and all interactive terminals wxt, x11, qt, windows, aqua, where the size is given in pixel. For vector-based terminals (postscript, svg, postscript etc) the size is given in inch or centimeters.
Using the -geometry flag works only for the x11 terminal:
gnuplot -geometry 800x800 -persist -e 'set terminal x11; plot x'
For all other pixel-based terminal you can use the size option to set the canvas size in pixel:
set terminal pngcairo size 800,800
Of course you can also extract the monitor resolution and use that as size. Here you have two variants:
Extract the monitor size on the shell:
monitorSize=$(xrandr | awk '/\*/{sub(/x/,",");print $1; exit}')
gnuplot -e "monitorSize='$monitorSize'; load 'gnuplotCode'"
The file gnuplotCode must then use the gnuplot variable monitorSize as follows:
set macros
set terminal pngcairo size #monitorSize
set output 'foo.png'
plot x
Note, that the content of the string variable monitorSize must be used as macro, i.e. the value is inserted before the whole line is evaluated.
If you don't want to have that additional line on the shell, you could also call the xrand stuff from within the gnuplot script via the system function. In that case the file gnuplotCode would look as follows:
monitorSize=system("xrandr | awk '/\*/{sub(/x/,\",\");print $1; exit}'")
set macros
set terminal pngcairo size #monitorSize
set output 'foobar.png'
plot x**2
which you must call only with gnuplot gnuplotCode.
Note, that the shell command as is always extracts the information of the first monitor only.

Clipping a geotiff file where it does NOT overlap with a shapefile

I have a geotiff file which overlaps with a shapefile. To clip for the overlapping part of the tif file, I can do this:
gdalwarp -co compress=deflate -dstnodata 255 -cutline shapefile.shp original.tif overlap.tif
But how can I clip for the non-intersecting part? That is, I want to create the complement of "overlap.tif" w.r.t. "original.tif".
You can use gdal_rasterize to burn a value where the shapefile overlaps the file. It works on an existing file, so make sure you use a copy.
gdal_rasterize -burn 255 shapefile.shp copy_of_original.tif
This burns a value of 255, setting -a_nodata 255 doesnt work on my version of GDAL. If you need it to be a real nodata value using gdal_translate with -a_nodata 255 afterwards would do the trick.
Gdal_rasterize also has a convenient -i flag which inverts the shapefile.