I have gotten a few emails asking how to install and use shp2text, a program I used in a previous post. This post will be a step by step guide on how to install shp2text on Mac OS X 10.4 - 10.6. A version of Xcode must also be installed on the machine for the code to compile. The original code provided by obviously.com will not compile properly, so here is my modified version that will compile on OS X.
First, unzip onto the Desktop. Then open up a terminal and type:
cd ~/Desktop/shp2text/
This command will change the terminal directory to the shp2text directory. If you input "ls" you should see the files in the shp2text directory like Makefile, dbfopen.c, and shapefil.h and etc. Now simple type:
make
and you should see an output that looks like:
cc -g -c shpopen.c
cc -g -c dbfopen.c
cc -g shpdiff.c shpopen.o dbfopen.o -o shpdiff
shpdiff.c: In function ‘compareDBF’:
shpdiff.c:539: warning: format ‘%s’ expects type ‘char *’, but argument 3 has type ‘char (*)[512]’
shpdiff.c:539: warning: format ‘%s’ expects type ‘char *’, but argument 4 has type ‘char (*)[512]’
cc -g shp2text.c shpopen.o dbfopen.o -o shp2text
Now to test if the compilation was done correctly, I have included the sample .shp file provided by obviously.com. To export the latitude and longitude information with shp2text type:
./shp2text --gpx test/bike-cape_islands-geo.shp 6 0 > test/output.xml
And you will find that an output.xml file, in the test directory, has been created with the .shp data in XML format. I hope these quick instructions help the confused people out there.
Here is the modified shp2text source code again.
Thursday, June 16, 2011
Wednesday, May 25, 2011
Chicago Crime
As a former resident of Chicago I was curious to how much crime happened in the period between Feb. 18, 2011 and May 15, 2011 and where crimes occurred. Of course this analysis could be done on EveryBlock, but that is just too easy. I wanted to do it in Mathematica, incase I want to do any type of advanced analysis later.
The GIS information for the city of Chicago can be found here. The data must then be unzipped and converted to an XML format, which can be done with shp2text. The data that is necessary to for this analysis is provided in the downloadable zip file found below.
The first step to the analysis is to extract the GIS data of the city. To extract the x and y coordinates that make up the polygons of the city of Chicago, the following command was used:
The data used here were of Chicago Police Distracts and historic neighborhoods of Chicago.
The next step was to access the EveryBlock API and gather the crime data. The API is free to access, you only need to register for a key to use it. A quirky characteristic about this API is the limited data that is provided. In the request command I have requested all crimes from January 1st, 2010 but the API will only provide crimes uploaded from the last 24 hours from EveryBlock. The command to access and isolate the crime data is:
The odd thing about this project is the coordinate disparity. The Chicago GIS data is provided in a NAD83 transformation rather than just latitudes and longitudes. This transformation gives x and y coordinates in units of feet rather than degrees and minutes. To convert the latitude and longitude data gathered from the EveryBlock API this Mathematica command was used:
Now let's get to some pretty pictures and results. For the period between Feb. 18th, 2011 to May 15th, 2011, the crime distribution in Chicago looked like this:
A count of crime incidents can be performed since we are doing this in Mathematica. The number of crime incidents in each neighborhood are listed in the table below:
The same analysis can be done with Chicago Police Districts. CPD publishes monthly crime statistics for each district, I have compiled this information for the month of January 2011 to add a bit more color to the crime map. In the crime map below, more red the color the higher the incidents of crime in the district.
And the accompanying crime incidents for each district is:
The datafiles used in this blog is provided here.
The GIS information for the city of Chicago can be found here. The data must then be unzipped and converted to an XML format, which can be done with shp2text. The data that is necessary to for this analysis is provided in the downloadable zip file found below.
The first step to the analysis is to extract the GIS data of the city. To extract the x and y coordinates that make up the polygons of the city of Chicago, the following command was used:
The data used here were of Chicago Police Distracts and historic neighborhoods of Chicago.
The next step was to access the EveryBlock API and gather the crime data. The API is free to access, you only need to register for a key to use it. A quirky characteristic about this API is the limited data that is provided. In the request command I have requested all crimes from January 1st, 2010 but the API will only provide crimes uploaded from the last 24 hours from EveryBlock. The command to access and isolate the crime data is:
The odd thing about this project is the coordinate disparity. The Chicago GIS data is provided in a NAD83 transformation rather than just latitudes and longitudes. This transformation gives x and y coordinates in units of feet rather than degrees and minutes. To convert the latitude and longitude data gathered from the EveryBlock API this Mathematica command was used:
Now let's get to some pretty pictures and results. For the period between Feb. 18th, 2011 to May 15th, 2011, the crime distribution in Chicago looked like this:
A count of crime incidents can be performed since we are doing this in Mathematica. The number of crime incidents in each neighborhood are listed in the table below:
The same analysis can be done with Chicago Police Districts. CPD publishes monthly crime statistics for each district, I have compiled this information for the month of January 2011 to add a bit more color to the crime map. In the crime map below, more red the color the higher the incidents of crime in the district.
And the accompanying crime incidents for each district is:
The datafiles used in this blog is provided here.
Monday, April 18, 2011
Google Finance Backfill
During a 3am google search, I stumbled upon this page:
http://www.marketcalls.in/database/google-realtime-intraday-backfill-data.html
It seems that google finance keeps a 15 day minute by minute backfill of all stock quotes. This was a great opportunity to use Mathematica to do some overly-complicated plotting at 3am.
The parameters to get the data is detailed on the site above. For the minute by minute quote of JP Morgan for the last 15 days, the Market Calls website suggested using the following URL:
http://www.google.com/finance/getprices?q=JPM&x=NYSE&i=60&p=15d&f=d,c,h,l
The first task was to create a way to download the data for any stock of interest, which was easy enough with Mathematica.
http://www.marketcalls.in/database/google-realtime-intraday-backfill-data.html
It seems that google finance keeps a 15 day minute by minute backfill of all stock quotes. This was a great opportunity to use Mathematica to do some overly-complicated plotting at 3am.
The parameters to get the data is detailed on the site above. For the minute by minute quote of JP Morgan for the last 15 days, the Market Calls website suggested using the following URL:
http://www.google.com/finance/getprices?q=JPM&x=NYSE&i=60&p=15d&f=d,c,h,l
The first task was to create a way to download the data for any stock of interest, which was easy enough with Mathematica.
Now the second task was a bit more complicated, since the backfill data kept time in a Unix format (counting the seconds since the birth of Unix). The following snippet of code can convert this Unix time to a more familiar format.
Then all that is left to do, is to extract and format the price data.
Now it is time to plot!
When insomnia hits again, I may try my hand on some econometrics styled analysis with this data. For those interested in playing with the code, you can download it here
Subscribe to:
Posts (Atom)











