Installing rgdal package for R 3

Day by day, my usage of R is increasing, it is simple, less code yet powerful. Recently I used R to get pixel values from a stack of images and generated graphs. Today i had to upgrade a server to R3 and ran into installation related issues

rgdal package installation was causing problems, error message was

gdal-config
--configure-args='--with-gdal-config=/usr/local/bin/gdal-config'
with appropriate values for your installation.

ERROR: configuration failed for package ‘rgdal’
* removing ‘/home/mycomputer/R/x86_64-pc-linux-gnu-library/3.0/rgdal’

The downloaded source packages are in
‘/tmp/Rtmp27M68g/downloaded_packages’

Warning message:
In install.packages("rgdal") :installation of package ‘rgdal’ had non-zero exit status

I didn’t have gdal-config on my computer and when i tried to install gdal-config following using command

sudo apt-get install libgdal1-dev

I got following errors

Reading package lists... Done
Building dependency tree 
Reading state information... Done
Some packages could not be installed. This may mean that you have
requested an impossible situation or if you are using the unstable
distribution that some required packages have not yet been created
or been moved out of Incoming.
The following information may help to resolve the situation:

The following packages have unmet dependencies:
libgdal1-dev : Depends: libdap-dev but it is not going to be installed
E: Unable to correct problems, you have held broken packages.

I checked my gdal version using synaptic (alternate gdalinfo – -version command) and it was 1.9 (and this was the issue, gdal 1.10 was required) thus added ppa from ubutnugis.  Started synaptic package manager and clicked ‘Repositories’ from settings menu, went to ‘Other Software ‘ tab and added following repository (one by one)

deb http://ppa.launchpad.net/ubuntugis/ubuntugis-unstable/ubuntu precise main

deb http://ppa.launchpad.net/ubuntugis/ubuntugis-unstable/ubuntu precise main (Source Code)

Now from Edit menu, clicked ‘Reload Package Information’ , gdal 1.9 required upgrade, once update was complete  the command below worked

sudo apt-get install libgdal1-dev

relax it will take some time. Finally i installed package rgdal on R using

install.packages('rgdal')  

I checked installation by executing my graph R script and it worked.

rain-fall

But I discovered an other issue (after some time), now an applications (geonode) requiring gdal is not working. I installed geonode on an other server and this time instead of the above process, I did not update gdal but simply used the following command (use it after installing R)

sudo apt-get install libgdal1-dev libgdal-dev

Finally, I installed rgdal package. Till now I have not seen any issues with applications (and gdal version has not changed).

 

 

Advertisements

Easily Generate PDFs in Python

A common task in any web application these days is generating files for user reports, the most common being PDFs.  I’ve been building a simple app to track assets and who’s been assigned what asset within a department, and I needed a PDF report of the main page that shows asset allocation.

Naturally, I went with xhtml2pdf since I wanted to take a HTML file, feed it with some context data, and return the output as the PDF.  This is all fine for basic table PDFs, but the moment you add anything fancy like border lines and background colours….things get a bit messed up layout.

After a few minutes of Google search, I came across weasyprint.  It renders the PDFs much better, exactly what is seen in the browser.  For installing this, the recommended way is to go through the CheeseShop as always:

pip install WeasyPrint

But for my Ubuntu 12.04 dev environment, I needed a few more extra plugins to get things installed:

sudo apt-get install libgdk-pixbuf2.0-0 libffi-dev

If these libraries are missing, you’ll get this error:

weasyprint-install-error

So after running through the setup…should take a couple of seconds to download and compile all the dependencies, you can now generate your PDF. I’m developing the system in Django, so here’s what I did:

from django.http import HttpResponse
from django.template import RequestContext, loader
from weasyprint import HTML

context_dict = {
'assets': Item.objects.all(),
}

template = loader.get_template('asset/pdf.html')
html = template.render(RequestContext(request, context_dict))
response = HttpResponse(mimetype='application/pdf')
HTML(string=html).write_pdf(response)

return response

Incase you have inline images using the static templatetag, you might want to make a few edits make a HTTP request on the app since WeasyPrint will have trouble determining the base URL. Note that it might cause a deadlock on a single-threaded server as pointed out in this StackOverflow post:


HTML(string=html, base_url=request.build_absolute_uri()).write_pdf(response)

After that, the result should be a pleasing to look at PDF supporting all sorts of CSS formatting options. For my deployment, I’m using inline CSS in my base template, but you can also feed in more CSS from the included CSS class. More details on this: Python API and StackOverflow

Of course your can always render the HTML directly without an extra library parsing it then return the plain HTML and have the user print the page as a PDF, but this means the user’s browser determines the output….Chrome and Firefox always do a good job here, but you never know what might happen down the line. But if this is OK for you, here’s what you can do:


context_dict = {
'assets': Item.objects.all(),
}
template_name = "asset/pdf.html"
return render_to_response(template_name, context_dict)

Wait, that’s not how it happened

Welcome to Script n Debug, your one stop shop for all cool scripts and bug fixes, covering a wide area of topics such as GIS, Linux, Python, R, Django, Software & Web Development, and the occasional gaming post.  We aim to post a range of articles that are helpful, informative, and easy to follow.

 

Thank you for stopping by, and if you like our posts here, feel free to share them on social networks or post comments.