Tuesday, June 2, 2015

How to set up a pc for geographers (continue updating)

A. Linkage between ArcGIS and Anaconda: Using ArcPy in Anaconda

1. Install ArcGIS, including ArcGIS for Desktop and ArcGIS for Desktop Background Geoprocessing (64 bit) (required, as large data processing is much more common)

2. Install Anaconda:
Add Anaconda to my PATH environment variable: convenient for using Anaconda in the Windows Command Processor by a command, ipython
Register Anaconda as my default Python 2.7: All python packages will save in the folder, where Anaconda is located, and will be different from python for ArcGIS. => in my opinion, this way avoids the direct dependency of commercial software, and is easy to maintain.

3. Copy the link file of ArcPy to the Anaconda folder
Copy DTBGGP64.pth from C:\Python27\ArcGISx6410.2\Lib\site-packages (depending where you save your python for ArcGIS)
Save DTBGGP64.pth under C:\Anaconda\Lib\site-packages (depending where you save your Anaconda)
Test whether ArcPy can work in Anaconda
(1) Type in cmd in the start menu to open Windows Command Processor
(2) Type in ipython to open Anaconda
(3) import arcpy => it may take appropriately five seconds to load arcpy

B. Linkage between R and Anaconda: Using R in Anaconda
1. Install R and Anaconda separately: 64 bit version is more recommended, as large data processing is much more common
R 64 bit: select 64 bit version when installation
Anaconda 64 bit: select Windows 64-Bit Python 2.7 Graphical Installer when download

2. Install the Rpy2 and singledispatch packages
Download the packages from Unofficial Windows Binaries for Python Extension Packages (http://www.lfd.uci.edu/~gohlke/pythonlibs/)
Install the packages within Windows Command Processor
(1) locate to the file, where the rpy and singledispatch packages saves by the command, e.g. cd /d c:/ (or any location of folder)
(2) install the packages by the command, pip install rpy2-2.5.6-cp27-none-win_amd64.whl & pip install singledispatch-3.4.0.3-py2.py3-none- any.whl
  
3. Add Paths
Open System properties by mouse right checks
Add two paths in System variables: R_HOME is to point out the location where R installed, while R_USER indicated the user name (Control panel -> Users Accounts and Family Safety -> User Accounts)
 
4. Test whether rpy2 works without any error message: type in the following codes
import rpy2.robjects as robjects
from rpy2.robjects.packages import importr
import rpy2.robjects.numpy2ri
rpy2.robjects.numpy2ri.activate()

C. Install packages in R
install.packages('ctv')
library('ctv')
install.views('Spatial')
install.views('SpatioTemporal')

D. install Postgis TIGER geocoder
1. Create extension
CREATE DATABASE geocoder;
CREATE EXTENSION postgis;
CREATE EXTENSION address_standardizer;
CREATE EXTENSION fuzzystrmatch;
CREATE EXTENSION postgis_topology;
CREATE EXTENSION postgis_tiger_geocoder;

2.  Prepare files
download postgis-2.2.0dev from http://postgis.net/stuff/postgis-2.2.0dev.tar.gz
Download Wegt and its Dependencies from http://gnuwin32.sourceforge.net/packages/wget.htm
-> extract file and put under C:/
-> move wget file from the bin folder to C:/wget
-> include Dependencies (four dll files) under C:/wget
install 7zip from http://www.7-zip.org/download.html

3.  Follow steps in the README file (under ~ postgis-2.2.0dev/extras/tiger_geocoder)
Format output: " IF YOU ARE AT A PSQL PROMPT, FIRST RUN "\a", "\t", AND "\o script.xxx".  THIS WILL MAKE YOUR OUTPUT UNALIGNED AND REDIRECT IT TO script.xxx.  WITHOUT "\a" and "\t", THE SCRIPT WILL HAVE EXTRA WHITESPACE AND POSSIBLY NON-SCRIPT CHARACTERS THAT CAUSE IT TO BREAK."
Download county, state tables and shapes at national level by using the command: SELECT loader_generate_nation_script('windows');  
Download other shapefiles at the state level by using the command: SELECT loader_generate_script(ARRAY['DC','RI'], 'windows');
=> the script can be changed to any state abbreviation
=> copy all the outputs into any text editor
=> save the two text outputs as .bat file under C:/ (should be under the root, or the path will be too long to download shapefiles)
=> run .bat files

4.  Test the geocoding function
Road: SELECT g.rating, ST_SRID(ST_Transform(g.geomout, 4326)) AS srid_wgs84 , ST_X(ST_Transform(g.geomout, 4326)) As lon_wgs84, ST_Y(ST_Transform(g.geomout, 4326)) As lat_wgs84, ST_SRID (g.geomout) AS srid_nad83, ST_X (g.geomout) AS lon_nad83, ST_Y(g.geomout) AS lat_nad83, (addy).address As stno, (addy).streetname As street, (addy).streettypeabbrev As styp, (addy).location As city, (addy).stateabbrev As st,(addy).zip FROM geocode('1701 S ELM, Norman OK') AS g order by g.rating;
Road Intersection: SELECT pprint_addy(addy), st_astext(geomout),rating FROM geocode_intersection('ANN ARBOR', 'BERRY RD', 'OK', 'Norman');