London Drug Map

A look into open data about crime and policing in England.
londondrugs banner
Found at https://data.police.uk/. I downloaded a date range from 2011 through 2016, ~5 years. The uncompressed filesize for all select-able policing forces was approximately 5.45 GB. A quick C# script was written to filter out only the “Drug” crime type category, and it’s long/lat degree coordinates.

The output was used to make the map below. Click map for larger size.
LondonDrugs_PoliceRaw

londonlarger

1more londonmap

The downloadable data comes in a multi-directory/multi-file structure. Below is a c# console application which was created to parse the root folder, and export the specific crime type’s coordinates throughout out all the files.

Console.BufferHeight = 4000;
string dirPath = @"C:\LONDON3\";
List<string> dirs = new List<string>(Directory.EnumerateDirectories(dirPath));
Dictionary<string, int> dictUniqueCrime = new Dictionary<string, int>();
string OUTPUTFILE = "date, longitude, latitude\r\n";
foreach (var dir in dirs)
{
    Console.WriteLine("{0}", dir.Substring(dir.LastIndexOf("\\") + 1));
    string filepath = @"C:\LONDON3\";
        filepath += dir.Substring(dir.LastIndexOf("\\") + 1);
    Console.WriteLine(filepath);
    string line;
    string[] fileEntries = Directory.GetFiles(filepath);
    foreach (string fileName in fileEntries)
    {
        Console.WriteLine(fileName);
        System.IO.StreamReader file =
            new System.IO.StreamReader(fileName);
        while ((line = file.ReadLine()) != null)
        {
            string[] parts = line.Split(',');
            //crime id
            //Console.WriteLine(parts[0]);
            //month
            //Console.WriteLine(parts[1]);
            string month = parts[1];
            //reported by
            //Console.WriteLine(parts[2]);
            //falls by
            //Console.WriteLine(parts[3]);
            //long
            //Console.WriteLine(parts[4]);
            string longitude = parts[4];
            //lat
            //Console.WriteLine(parts[5]);
            string latitude = parts[5];
            //loc
            //Console.WriteLine(parts[6]);
            //code
            //Console.WriteLine(parts[7]);
            //name
            //Console.WriteLine(parts[8]);
            //crime type
            //Console.WriteLine("crime type ::::;  "  + parts[9]);
            string crimetype = parts[9];
            // Console.WriteLine(parts[10]);
            if (crimetype == "Drugs")
            {
                if ( dictUniqueCrime.ContainsKey(crimetype) )
                {
                    int existing = dictUniqueCrime[crimetype];
                    existing += 1;
                    dictUniqueCrime[crimetype] = existing;
                }
                else
                {
                    dictUniqueCrime.Add(crimetype, 1);
                }
                string lines =  month + ", " + longitude + ", " + latitude+"\r\n";
                System.IO.StreamWriter file1 = new System.IO.StreamWriter("C:\\Airplanes\\____2011-2016ALL UK DRUGS.csv", true);
                file1.WriteLine(lines);
                file1.Close();
            }
        }
        file.Close();
    }
}
var items = from pair in dictUniqueCrime
            orderby pair.Value ascending
            select pair;
foreach (KeyValuePair<string, int> pair in items)
{
    Console.WriteLine("{0}: {1}", pair.Key, pair.Value);
}

Leave a Reply

Your email address will not be published. Required fields are marked *