Example #1
0
    def set_vector_file(self,
                        file_name,
                        inten_name=['intensity'],
                        dst_crs=None):
        """ Read vector file format supported by fiona. Each intensity name is
        considered an event. Returns intensity array with shape
        (len(inten_name), len(geometry)).

        Parameters:
            file_name (str): vector file with format supported by fiona and
                'geometry' field.
            inten_name (list(str)): list of names of the columns of the
                intensity of each event.
            dst_crs (crs, optional): reproject to given crs

        Returns:
            np.array
        """
        if not self.geometry.crs:
            self.lat, self.lon, self.geometry, inten = read_vector(file_name, \
                inten_name, dst_crs)
            return sparse.csr_matrix(inten)
        tmp_lat, tmp_lon, tmp_geometry, inten = read_vector(file_name, \
            inten_name, dst_crs)
        if not equal_crs(tmp_geometry.crs, self.geometry.crs) or \
        not np.allclose(tmp_lat, self.lat) or\
        not np.allclose(tmp_lon, self.lon):
            LOGGER.error('Vector data inconsistent with contained vector.')
            raise ValueError
        return sparse.csr_matrix(inten)
Example #2
0
    def set_vector_file(self,
                        file_name,
                        inten_name=['intensity'],
                        dst_crs=None):
        """Read vector file format supported by fiona.

        Each intensity name is considered an event.

        Parameters
        ----------
        file_name : str
            vector file with format supported by fiona and 'geometry' field.
        inten_name : list(str)
            list of names of the columns of the intensity of each event.
        dst_crs : crs, optional
            reproject to given crs

        Returns
        -------
        inten : scipy.sparse.csr_matrix
            Sparse intensity array of shape (len(inten_name), len(geometry)).
        """
        if not self.geometry.crs:
            self.lat, self.lon, self.geometry, inten = u_coord.read_vector(
                file_name, inten_name, dst_crs)
            return sparse.csr_matrix(inten)
        tmp_lat, tmp_lon, tmp_geometry, inten = u_coord.read_vector(
            file_name, inten_name, dst_crs)
        if not (u_coord.equal_crs(tmp_geometry.crs, self.geometry.crs)
                and np.allclose(tmp_lat, self.lat)
                and np.allclose(tmp_lon, self.lon)):
            LOGGER.error('Vector data inconsistent with contained vector.')
            raise ValueError
        return sparse.csr_matrix(inten)
    def test_read_vector_pass(self):
        """ Test one columns data """
        shp_file = shapereader.natural_earth(resolution='110m', \
            category='cultural', name='populated_places_simple')
        lat, lon, geometry, intensity = read_vector(shp_file, ['pop_min', 'pop_max'])

        self.assertEqual(geometry.crs, from_epsg(NE_EPSG))
        self.assertEqual(geometry.size, lat.size)
        self.assertEqual(geometry.crs, from_epsg(NE_EPSG))
        self.assertAlmostEqual(lon[0], 12.453386544971766)
        self.assertAlmostEqual(lon[-1], 114.18306345846304)
        self.assertAlmostEqual(lat[0], 41.903282179960115)
        self.assertAlmostEqual(lat[-1], 22.30692675357551)

        self.assertEqual(intensity.shape, (2, 243))
        # population min
        self.assertEqual(intensity[0, 0], 832)
        self.assertEqual(intensity[0, -1], 4551579)
        # population max
        self.assertEqual(intensity[1, 0], 832)
        self.assertEqual(intensity[1, -1], 7206000)