Latitude/Longitude of each pixel using python and gdal

I needed lat/long of each pixel of a GeoTiff file. I searched through internet to find a solution and ,thanks to Stack Overflow, found a piece of code that I modified. As usual, python and gdal were used. Just follow the instructions below.

First get the number of rows and columns of you image by using gdalinfo (although it can be automated as well but lets just go with it)

gdalinfo raster.tif

Now edit the python code below and set rows and columns according to your image (don’t forget to change the image name).

from osgeo import gdal

# Open tif file
ds = gdal.Open('raster.tif')

# GDAL affine transform parameters, According to gdal documentation xoff/yoff are image left corner, a/e are pixel wight/height and b/d is rotation and is zero if image is north up. 
xoff, a, b, yoff, d, e = ds.GetGeoTransform()

def pixel2coord(x, y):
 """Returns global coordinates from pixel x, y coords"""
 xp = a * x + b * y + xoff
 yp = d * x + e * y + yoff
 return(xp, yp)

# get columns and rows of your image from gdalinfo
rows = 36+1
colms = 34+1

if __name__ == "__main__":
 for row in  range(0,rows):
  for col in  range(0,colms): 
   print pixel2coord(col,row)

Finally, execute this python script using following command (provided that you have named your file script.py)

python script.py

Here is the output of my raster.tif

(29.25, 6.0)
(29.5, 6.0)
(29.75, 6.0)
(30.0, 6.0)
(30.25, 6.0)
(30.5, 6.0)
(30.75, 6.0)
(31.0, 6.0)
(31.25, 6.0)
(31.5, 6.0)
.
.
.

In my case, I converted output to csv and opened it in qGIS along with raster.tif (shown below).

for_fb2

and a closer look

for_fb

 

Advertisements

Notepadqq – great Notepad++ alternative on Ubuntu

I’ve recently had to switch by dev setup from Windows+VMWare to working from Ubuntu since I needed to use Vagrant+Ansible, and that dev setup never plays well with Windows.  This meant having to go through a new setup and look for alternatives to most of my favourite Windows tools.  One of these was Notepad++ I could of course use Wine and emulate Windows, but I felt there was an existing package that can run on Ubuntu without going through Wine.  After a brief search on Google, I came across Notepadqq.  It’s an opensource (free as in speech) editor for the Linus desktop that has a Notepad++ look and feel.

Screenshot from 2014-11-11 13:09:32

Some of the features include:

  • Syntax highlighting for about 100 different languages.
  • Code folding
  • Colour schemes
  • File monitoring
  • Multiple selection
  • Search text using the power of regular expressions
  • Organize documents side by side
  • Real-time highlighting to find near identifiers in no time.

To install the latest version (0.39.0 at the time of writing), you need to follow these steps:

Install Notepadqq 0.39.0 on Ubuntu 14.04 & Ubuntu 14.10
sudo add-apt-repository ppa:notepadqq-team/notepadqq
sudo apt-get update
sudo apt-get install notepadqq

Install Notepadqq 0.39.0 on Debian OS (change version number as per the desired one)
sudo apt-get install gdebi

For 32bit OS
wget "https://launchpad.net/~notepadqq-team/+archive/ubuntu/notepadqq/+files/notepadqq_0.39.0-1%7Etrusty1_i386.deb" -O notepadqq_0.39.0_i386.deb
sudo gdebi notepadqq_0.39.0_i386.deb

For 64bit OS
wget "https://launchpad.net/~notepadqq-team/+archive/ubuntu/notepadqq/+files/notepadqq_0.39.0-1%7Etrusty1_i386.deb" -O notepadqq_0.39.0_amd64.deb
sudo gdebi notepadqq_0.39.0_amd64.deb

After that, you should have your text editor ready for use 🙂

Proj4 and python

Proj4 is a projection library used by many software including GRASS, MapServer and PostGIS (source). For python, one has to install package pyproj.

Few days back, we were trying to georeference some aerial photos (around 7000). The aerial photos were accompanied by an excel sheet with cells like image id, latitude and longitude (image’s center).

But there was an issue, we were georeferencing images using source maps with projection EPSG:3857 (a.k.a the web projection). Every time we georeference an image, lat long to x y conversion was required (geographic to projected conversion).

To solve this, I converted excel sheet into csv (because parsing excel sheets with python is a bit too much for me) and wrote a small python script to transform coordinates. Following is python code.

import csv
import pyproj

# read from csv
cr = csv.reader(open("Aerial_images.csv","rb"))

# writer to csv
csvfile = open('Aerial_images-3857.csv', 'wb') 
csv_writer = csv.writer(csvfile, delimiter='~',quotechar='|', quoting=csv.QUOTE_MINIMAL)

#source 
p1 = pyproj.Proj(proj='latlong',datum='WGS84')

# final projectoin
p2 = pyproj.Proj(init='epsg:3857')

for row in cr: 
  print row[0], row[1], row[2], row[3]
  x1=float(row[2]) 
  y1=float(row[3]) 

  x2, y2 = pyproj.transform(p1,p2,x1,y1)
  #'%9.3f %11.3f' % (x2,y2)

  str_temp = row[0], row[1], row[2], row[3], x2,y2 
  csv_writer.writerow(str_temp)

All it took was few seconds and conversion was done (converted points below).

aerial-images

 

Resize virtual machine’s desktop in VirtualBox

virtual_box_by_nicowolveus-d3lj23v

I started using VirtualBox (actively) few months back, my experience with it is excellent. Before using VirtualBox, I always had issues with installing new software on my computer because I did not want the new installation/updates to mess around with my working setups. VistualBox solved all of these issues. Now, I do all of my software testing/evaluations on virtual machines even I do all of my development (which require installations e.g. geonode) or host servers on virtual machines as well (read my blog ‘Access services hosted on guest from host using VirtualBox‘). Small tweaks here and there (like the one blow), VirtualBox is an excellent solution for developers as well.

If you have noticed, Virtual Box does not show fullscreen on virtual machines (VMs) and I hate to work on small screens (even I do not work on 15 inch and went to great lengths to get a 17 inch laptop). The fullscreen mode of VirtualBox is not what I am looking for, I need a solution which utilizes the available screen size, same goes for scalemode. Searched Google and found an answer on askubuntu (http://askubuntu.com/questions/104440/how-do-you-resize-the-standard-ubuntu-desktop-inside-of-virtualbox). To summarize, need to install following packages

sudo apt-get install virtualbox-guest-dkms virtualbox-guest-utils virtualbox-guest-x11

It worked on one of my VMs but then some how on my second VM the package virtualbox-guest-dkms (and others) could not be downloaded with error 404 Not Found. Frustrated, took the task to Synaptic Package manager. I search package virtualbox-guest-dkms and istalled it (that also installed virtualbox-guest-utils virtualbox-guest-x11). Restarted my VM and it worked.

Confused between apt-get and synaptic, I searched the difference and found this answer (http://askubuntu.com/questions/76/whats-the-difference-between-package-managers)

😦 the two sound same to me. I wonder what went wrong with apt-get

Note: This might not be the proper solution, but its what I experienced and it worked for me. Enjoy 🙂

Access services hosted on guest from host using VirtualBox

vm

VirtualBox, it solved a lot of my worries for example the issues of updating libraries and then finding that now some software are not working etc. Plus the idea of visualization is good as with it one can distribute resources (memory, CPU cores) among virtual machines thus can run a cluster of serves, in future (I will write about one such cluster). It also solves my development related issues as explained below.

I needed a webserver with php for a project. My idea was that I will have the webserver (with packages like php etc and php development) on a virtual machine. I will access this webserver from host to edit scripts (for this folder sharing is required between guest and host) and test php pages. I tried few things and finally found this excellent blog(link). It explains every thing (just follow it).

Second,to access guest folder from host (so that I can do development on host and save it in guest), I shared a guest folder using samba (you might have to install it).

Note: For those of you who do not understand guest and host with respect to VirtualBox, host is the computer on which VirtualBox is installed and guests are virtual machines that are created using VirtualBox. Once virtual machines(guests) are created, operating system and other software are installed.

I am following the same setup with my geonode testing/development virtual machine (developing tools in Geoexplorer using JavaScript, will write about it some day 🙂 ).