Showing posts with label latitude. Show all posts
Showing posts with label latitude. Show all posts

Monday, May 7, 2012

Chicago Crime - Updated

I was a bit dismayed to discover that the EveryBlock API stopped working. One of my most popular posts, Chicago Crime, depended on this API heavily.

Here I present an alternative data source for my previous post and a fun update. The update is a quick and dirty method to create popular crime density plots, as shown below.


In the spirit of transparency, the city of Chicago has taken the initiative to electronically release a great deal of data with Socrata Open Data API (SODA). Luckily for us, this release includes all filed police reports. The records available through this API range from the year 2001 to the present, which dwarfs the two week range provided by EveryBlock's API. However, I would not recommend downloading all of this data as the text file is approximately 2GB in size.

In this post I use only the filed police reports from one year prior to present. The data is available in both JSON and CSV format. I only use the CSV format here for the convenience of those with an earlier version of Mathematica. To import this data into Mathematica:





A plot of all reported crimes in Chicago between February 1st and March 31st of 2012 was created with the data.


Plot of incidents for any period within the year can be easily created with the following Mathematica code. Restrict the data to the range of interest by replacing the dates of the start and end strings. Dates must be in the format of mm/dd/yyyy. This code simply finds the positions of the strings that match the inputed dates and plots the daily totals of incidents.










The data can be parsed into primary descriptions including arson, homicide, sex offense, weapons violation, crim sexual assault, interfere with public officer, gambling, public peace violation, criminal trespass, liquor law violation, assault, deceptive practice, burglary, criminal damage, robbery, motor vehicle theft, offense involving children, narcotics, theft, other offense, and battery. Below is the plot and code to filter for only narcotics incidents.






















A secondary description can also be used to parse the data. Some secondary descriptions include aggravated: handgun, poss: crack, armed: handgun, harassment by telephone, poss: heroin(white), to land, retail theft, unlawful entry, strongarm - no weapon, to property, poss: cannabis 30gms or less, from building, $500 and under, forcible entry, to vehicle, domestic battery simple, simple, automobile, over $500, telephone threat, and wireroom/sports. Below is the plot and code to filter for incidents with a primary description of narcotics and a secondary description of poss: cannabis 30gms or less.























The incidents may also be separated into ones where arrests were made and ones where no arrests were made. Below is code to filter for incidents with a primary description of narcotics and a secondary description of poss: cannabis 30gms or less with arrests and without. Both plots comparing to total narcotics incidents are shown.





















The method to display where certain incidents occur in chicago is similar to my previous post, Chicago Crime. Please refer to that posting for a more detailed explanation on how to create the plot below. The plot here is a geographical visualization of where arrests occurred relating to incidents of narcotics with possession of cannabis with 30 grams or less.


The method to tabulate the number of incidents within neighborhoods is also similar to my previous post, Chicago Crime. This calculation totals the numbers of incidents within each neighborhood boundary. It is the most costly in computational time and power because of the generality in the counting code that allows it to use any boundaries (such as census tracts, police beats, etc.) The tabulation of arrests made relating to incidents of narcotics with possession of cannabis with 30 grams or less in each neighborhood is presented.


While doing some initial research for this post, I found "crime hotspots" as a popular visualization option. A similar visualization is easily created in mathematica with three simple lines of code.













The Mathematica code used in this post can be downloaded here. Some fun analysis of crimes in chicago will follow in future posts.

Thursday, June 16, 2011

Exporting .shp Files into XML with shp2text

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.