Example #1
0
    def __init__(self, address = None):

        if address:
            if not self._is_in_chicago(address):
                raise ValueError('Address not in Chicago')

        else:
            self.get_address()

        self.formatted_address = self.address_info['formatted_address']
        self.lat,self.lon = self._get_address_lat_lon()
        self.district = get_district_from_lat_lon((self.lat,self.lon))
        self.street_address = self._get_street_address()

        police_station_lat, police_station_lon  = get_police_station_coordinates(self.district)
        self.distance_to_police_station = calculate_distance_between_points(self.lat,self.lon, police_station_lat, police_station_lon)

        self.summary = None
Example #2
0
    def test_police_station_coordinates(self):
        #This part of the test will pass if no exceptions are raised
        for district in self.valid_districts:
            databases_utils.get_police_station_coordinates(district)

        #This part of the test will pass if ValueError exception is raised
        for district in self.invalid_districts:
            with self.assertRaises(ValueError):
                databases_utils.get_police_station_coordinates(district)

        for district in self.malformed_districts:
            with self.assertRaises(TypeError):
                databases_utils.get_police_station_coordinates(district)
    def test_police_station_coordinates(self):
        #This part of the test will pass if no exceptions are raised
        for district in self.valid_districts:
            databases_utils.get_police_station_coordinates(district)

        #This part of the test will pass if ValueError exception is raised
        for district in self.invalid_districts:
            with self.assertRaises(ValueError):
                databases_utils.get_police_station_coordinates(district)

        for district in self.malformed_districts:
            with self.assertRaises(TypeError):
                databases_utils.get_police_station_coordinates(district)
Example #4
0
 def test_try_open_wrong_DB(self):
     with self.assertRaises(IOError):
         databases_utils.get_police_station_coordinates(
             self.valid_districts[0], db_police='xxx')
 def test_try_open_wrong_DB(self):
     with self.assertRaises(IOError):
         databases_utils.get_police_station_coordinates(self.valid_districts[0],db_police='xxx')
Example #6
0
        def district_mapper(crimes, address_object, show_district_block=True):
                """Create and shows a Custom Heat map of a particular district of Chicago city. Heat will be based on crime density,
                and each individual crime will be a translucent blue point. Police station will be plotted as a violet
                point, and the particular address as a black point.

                Several parts of the code on how to create a Heat map, where inspired by the example found in
                http://bagrow.com/dsv/heatmap_basemap.html. However, they were highly customized and modified for our
                particular use case.

                :param crimes: list or tuple of lists or tuples of coordinates for crimes.
                :param address_object: Receives an instance of the Address class from the addressClass module
                """

                # Validate input
                mu._district_mapper_input_validator(crimes, address_object)

                # Get the district number, longitude and latitude of the address of interest, and string representation of the street address.
                district_number, lat_address, lon_address, street_address = address_object.district, address_object.lat, address_object.lon, address_object.street_address

                # Get proper limits for the district and create limits for the map based on the maximum and minimum latitudes of the district plus a certain margin.
                min_lon, max_lon, min_lat, max_lat, district_limits_lon, district_limits_lat, vertices_list = mu._district_map_limits_calculator(district_number)

                # Filter the crimes database to keep only crimes inside the map space.
                filtered_crimes = gu.return_points_in_square(crimes, min_lat, max_lat, min_lon, max_lon)

                # Save the coordinates of the crimes to plot in two lists.
                lat_crimes_list, lon_crimes_list = mu._crimes_lon_lat_splitter(filtered_crimes)

                plt.close('all')

                # Create a basemap instance of the district with the limits that were established before.
                district_map = mu._basemap_district(min_lat, min_lon, max_lat, max_lon)

                # Generate bins for the Heatmap, calculate densities and smooth densities
                lat_bins_2d, lon_bins_2d, smoothed_density = mu._bin_densities_calculator(lat_crimes_list, lon_crimes_list)

                # Convert the bin mesh to map coordinates:
                xs, ys = district_map(lon_bins_2d, lat_bins_2d)

                # Add histogram squares and a corresponding colorbar to the map:
                plt.pcolormesh(xs, ys, smoothed_density, cmap=cm.OrRd)


                # Add colorbar into the map
                cbar = plt.colorbar(orientation='horizontal', shrink=0.625, aspect=20, fraction=0.2, pad=0.02)
                cbar.set_label('Number of crimes in district number ' + str(district_number), size=18)


                # Translucent blue scatter plot of Crimes above histogram:
                x_crimes, y_crimes = district_map(lon_crimes_list, lat_crimes_list)
                district_map.plot(x_crimes, y_crimes, 'o', markersize=1, zorder=6, markerfacecolor='#424FA4', markeredgecolor="none", alpha=0.33)

                # Get the coordinates of the Police Station of the District and plot it.
                police_coordinates = databases_utils.get_police_station_coordinates(district_number)
                x_police, y_police = district_map(police_coordinates[1], police_coordinates[0])

                district_map.plot(x_police, y_police, marker='D', color='m')
                plt.annotate('Police Station', xy=(x_police, y_police), xycoords='data', xytext=(district_map(police_coordinates[1]+0.001, police_coordinates[0]+0.001)), textcoords='data', color='m')

                # Get the coordinates of the address and plot it
                x_address, y_address = district_map(lon_address, lat_address)

                district_map.plot(x_address, y_address, marker='D', color='#7FFF00')

                plt.annotate(street_address, xy=(x_address, y_address), xycoords='data', xytext=(district_map(lon_address + 0.001, lat_address + 0.001)), textcoords='data', color='k')

                # Plot the boundaries of the Districts
                district_map.readshapefile('./Databases/districts/geo_fthy-xz3r-1', 'district_m')

                # Highlight the boundaries of our District of interest with black.
                ds_1, ds_2 = district_map(district_limits_lon, district_limits_lat)
                district_map.plot(ds_1, ds_2, linewidth=2, color='#000000')

                # Set the size of the image and show the plot
                plt.gcf().set_size_inches(15, 15)
                plt.show(block=show_district_block)