One of the more common ways that I read vector data into R is via shapefiles. I tend to use these partly becuase of my own sordid past with Arc/INFO, ArcView and ArcGIS and partly due to their ubiquity. In any event I have found the R package,
rgdal, indespensible for this. One of the workhorse functions for pulling in vector data is
readOGR(). It has two required parameters
layer. The part I never remember is how these relate to shapefiles. There is nothing especially tricky about it, I just tend to forget what the
dsn is and what the
layer is. In short the
dsn is the directory (without a trailing backslash) and the
layer is the shapefile name without the
So, here’s the actual code so I don’t have to look it up again.
If the shapefile you are reading is in your current working directory the
dsn refers simply to that directory. So all you need is simply a “.”. The layer is the name of shapefile without an extension. So it would look something like:
Now, if that file resides elsewhere, the trick is to remember what the heck
dsn refers to. Again it is simply the directory where the shapefile resides. So if I had a shapefile in a place like
C:/data I would use the command like:
There, it is really quite simple, yet I always mess it up. Not any more.
I can never remember how to do simple things, be it in Python, R, or Java (hope I don’t have to do too much more work in Java…). Given that, I plan to post these kind of things, when I remember of course, under the title of “Things I Forget”. My primary aim for this is to help myself, but I do hope others with a similar memory affliction will also find these useful.
For this first installment: How do you add new python packages from a tarball (i.e. .tgz or .tar.gz file) on Windows?
- Hope there is a windows installer. Given how unlikely this appears to be, move to step 2
- Download the tarball. This is the thing you always see when you are expecting to find the windows installer. It has some name followed by a .tar.gz or a .tgz. For instance, if you want to add the ability to write out Excel files (by the way, I am not advocating for Excel), then you could use the xlwt package. I usually download these files directly into my into my site-packages directory (C:/Your Python Path/Lib/site-packages).
- Once you download the taball you need to find someway to extract the file. On linux machines this is easy. I had a bit more of challenge getting it to work on a windows machine. There are a few options. I have used PowerArchiver before, but it costs. Cygwin can do it, but that may be overkill. The one I have used most recently is the very simple TarTool. It is free and doesn’t need to be installed, only extracted. I also keep the TarTool.exe in my site-packages directory. So, now that I have my tool to extract the file and the file itself, I simply do the following (click for full size):
- Now you will have a directory for the package, so to install it is simply a matter of changing into that directory and running the setup. Just like this (click for full size):
- Your python package is now ready to roll and can be imported (click for full size)!
As an aside, anyone know of a wordpress trick to display a DOS command prompt? Inserting a screen shot was the best I came up with, but it would be nice to have something akin to the method for highlighting sourcecode.