class DuplicateFindingTestCase(unittest.TestCase):
    """
    It loads two catalogues in different format. The first one is an
    ISF bulletin, with the measures already grouped (by the event
    source key). The second one contains the same set of measures but
    in IASPEI format. We apply the sequential grouping to see if the
    created group matches the group present in the ISF bulletin.
    """

    def setUp(self):
        self.cat = CatalogueDatabase(memory=True, drop=True)

        isf_bulletin_filename = "isf_two_events.txt"
        iaspei_filename = "iaspei_from_isf.csv"

        self.cat.load_file(in_data_dir(isf_bulletin_filename), "isf_bulletin")
        self.cat.load_file(in_data_dir(iaspei_filename), "iaspei")

    def test_match(self):
        grouper = GroupMeasuresBySequentialClustering(
            time_window=10, space_window=200)

        groups = grouper.group_measures(C())

        self.assertEqual(2, len(groups.values()))

        actual_groups = set([frozenset(measures)
                             for measures in groups.values()])

        expected_groups = set([frozenset(C(before=datetime(1952, 1, 1))),
                               frozenset(C(after=datetime(1997, 1, 1)))])

        self.assertEqual(expected_groups, actual_groups)
Пример #2
0
    def show_pippo2(self):
        dbfile = '/home/michele/pippo.db'
        db = CatalogueDatabase(filename=dbfile)
        agencies = db.get_agencies()
        one_agency = agencies.pop()  # example PRA
        data = filtering.WithAgencies([one_agency])
        uri = QgsDataSourceURI()

        display_name = 'Origin'
        uri = 'Point?crs=epsg:4326&index=yes&uuid=%s' % uuid.uuid4()
        vlayer = QgsVectorLayer(uri, display_name, 'memory')
        QgsMapLayerRegistry.instance().addMapLayer(vlayer)

        provider = vlayer.dataProvider()
        vlayer.startEditing()
        provider.addAttributes([
            QgsField('agency', QVariant.String),
            QgsField('event_name', QVariant.String),
            QgsField('event_measure', QVariant.String),
        ])
        features = []
        for row in data:
            feat = QgsFeature()
            x, y = row.origin.position_as_tuple()
            geom = QgsGeometry.fromPoint(QgsPoint(x, y))
            feat.setGeometry(geom)
            feat.setAttributes([QVariant(str(row.agency)),
                                QVariant(row.event.name),
                                QVariant(str(row.event.measures[0]))])
            features.append(feat)
        provider.addFeatures(features)
        vlayer.commitChanges()
        vlayer.updateExtents()
	self.iface.mapCanvas().setExtent(vlayer.extent())		
	vlayer.triggerRepaint()
Пример #3
0
 def update_catalogue_db(self, db_filename):
     self.catalogue_db = CatalogueDatabase(filename=db_filename)
     agencies = list(self.catalogue_db.get_agencies())
     mscales = list(self.catalogue_db.get_measure_scales())
     dates = self.catalogue_db.get_dates()
     self.dock.set_agencies(agencies)
     self.dock.set_magnitude_scales(mscales)
     self.dock.set_dates(dates)
    def setUp(self):
        self.cat = CatalogueDatabase(memory=True, drop=True)

        isf_bulletin_filename = "isf_two_events.txt"
        iaspei_filename = "iaspei_from_isf.csv"

        self.cat.load_file(in_data_dir(isf_bulletin_filename), "isf_bulletin")
        self.cat.load_file(in_data_dir(iaspei_filename), "iaspei")
Пример #5
0
    def show_pippo1(self, agencies=None):
        dbfile = '/home/michele/pippo.db'
        db = CatalogueDatabase(filename=dbfile)
	if agencies is None:
		agencies = db.get_agencies()
	data = filtering.WithAgencies(agencies)
	uri = QgsDataSourceURI()
	uri.setDatabase(dbfile)
	schema = ''
	table = 'catalogue_origin'
        geom_column = 'position'
        uri.setDataSource(schema, table, geom_column)

        display_name = 'Origin'
        vlayer = QgsVectorLayer(uri.uri(), display_name, 'spatialite')
        QgsMapLayerRegistry.instance().addMapLayer(vlayer)
        ids = tuple(row.origin.id for row in data)
        vlayer.setSubsetString('id in %s' % str(ids))
        vlayer.triggerRepaint()
Пример #6
0
class EqCatalogue:
    def __init__(self, iface):
        # Save reference to the QGIS interface
        self.iface = iface
        # initialize plugin directory
        self.plugin_dir = QFileInfo(QgsApplication.qgisUserDbFilePath()).path() + "/python/plugins/eqcatalogue"
        # initialize locale
        localePath = ""
        locale = str(QSettings().value("locale/userLocale"))[0:2]
        self.dockIsVisible = True

        if QFileInfo(self.plugin_dir).exists():
            localePath = self.plugin_dir + "/i18n/eqcatalogue_" + locale + ".qm"

        if QFileInfo(localePath).exists():
            self.translator = QTranslator()
            self.translator.load(localePath)

            if qVersion() > "4.3.3":
                QCoreApplication.installTranslator(self.translator)

        # Create the dialog (after translation) and keep reference
        self.dock = Dock(self.iface, gemcatalogue=self)
        self.catalogue_db = None
        self.data_dir = os.path.abspath(os.path.join(os.path.dirname(__file__), "data"))

    def initGui(self):
        # Create action that will start plugin configuration
        self.show_catalogue_action = QAction(
            QIcon(":/plugins/eqcatalogue/icon.png"), u"Eqcatalogue Toggle Dock", self.iface.mainWindow()
        )
        self.show_catalogue_action.setCheckable(True)
        self.show_catalogue_action.setChecked(self.dockIsVisible)

        self.import_action = QAction(
            QIcon(":/plugins/eqcatalogue/icon.png"), u"Import catalogue file in db", self.iface.mainWindow()
        )

        self.get_platform_settings = QAction(
            QIcon(":/plugins/eqcatalogue/icon.png"), u"Settings for the GEM platform", self.iface.mainWindow()
        )

        # connect the action to the run method
        QObject.connect(self.dock, SIGNAL("visibilityChanged(bool)"), self.update_toggle_status)
        QObject.connect(self.show_catalogue_action, SIGNAL("triggered()"), self.toggle_dock)
        self.import_action.triggered.connect(self.show_import_dialog)
        self.get_platform_settings.triggered.connect(self.platform_settings)

        # Add toolbar button and menu item
        self.iface.addToolBarIcon(self.show_catalogue_action)
        self.iface.addPluginToMenu(u"&eqcatalogue", self.show_catalogue_action)
        self.iface.addPluginToMenu(u"&eqcatalogue", self.import_action)
        self.iface.addPluginToMenu(u"&eqcatalogue", self.get_platform_settings)

        self.iface.addDockWidget(Qt.RightDockWidgetArea, self.dock)

        # display world map
        QObject.connect(self.iface, SIGNAL("initializationCompleted()"), self.load_countries)

    def unload(self):
        # Remove the plugin menu item and icon
        self.dock.close()
        self.iface.removeToolBarIcon(self.show_catalogue_action)
        self.iface.removePluginMenu(u"&eqcatalogue", self.show_catalogue_action)
        self.iface.removePluginMenu(u"&eqcatalogue", self.import_action)
        self.iface.removePluginMenu(u"&eqcatalogue", self.get_platform_settings)

    def toggle_dock(self):
        # show the dock
        self.dockIsVisible = not self.dockIsVisible
        self.dock.setVisible(self.dockIsVisible)

    def update_toggle_status(self, status):
        self.dockIsVisible = status
        self.show_catalogue_action.setChecked(status)

    def update_catalogue_db(self, db_filename):
        self.catalogue_db = CatalogueDatabase(filename=db_filename)
        agencies = list(self.catalogue_db.get_agencies())
        mscales = list(self.catalogue_db.get_measure_scales())
        dates = self.catalogue_db.get_dates()
        self.dock.set_agencies(agencies)
        self.dock.set_magnitude_scales(mscales)
        self.dock.set_dates(dates)

    def create_db(self, catalogue_filename, fmt, db_filename):
        cat_db = CatalogueDatabase(filename=db_filename)
        parser = FMT_MAP[fmt]
        self.dock.enableBusyCursor()
        try:
            with open(catalogue_filename, "rb") as cat_file:
                store_events(parser, cat_file, cat_db)
            self.dock.update_selectDbComboBox(db_filename)
        finally:
            self.dock.disableBusyCursor()
        return cat_db

    def show_import_dialog(self):
        self.import_dialog = ImporterDialog(self.iface)
        if self.import_dialog.exec_():
            self.create_db(
                self.import_dialog.import_file_path, str(self.import_dialog.fmt), self.import_dialog.save_file_path
            )

    def platform_settings(self):
        dialog = PlatformSettingsDialog(self.iface)
        dialog.exec_()

    def show_exposure(self, hostname, username, password):
        self.dock.enableBusyCursor()
        try:
            crs = self.iface.mapCanvas().mapRenderer().destinationCrs()
            xform = QgsCoordinateTransform(crs, QgsCoordinateReferenceSystem(4326))
            extent = xform.transform(self.dock.selectedExtent())
            lon_min, lon_max = extent.xMinimum(), extent.xMaximum()
            lat_min, lat_max = extent.yMinimum(), extent.yMaximum()

            # download data
            ed = ExposureDownloader(hostname)
            ed.login(username, password)
            try:
                fname = ed.download(lat_min, lon_min, lat_max, lon_max)
            except ExposureDownloadError as e:
                self.dock.disableBusyCursor()
                QMessageBox.warning(self.dock, "Exposure Download Error", str(e))
                return
            # don't remove the file, otherwise there will concurrency problems
            uri = "file://%s?delimiter=%s&xField=%s&yField=%s&crs=epsg:4326&" "skipLines=25&trimFields=yes" % (
                fname,
                ",",
                "lon",
                "lat",
            )
            vlayer = QgsVectorLayer(uri, "exposure_export", "delimitedtext")
            QgsMapLayerRegistry.instance().addMapLayer(vlayer)
        finally:
            self.dock.disableBusyCursor()

    def update_map(self, agencies_selected, mscales_selected, mag_range, date_range, selected_extent):
        filter_agency = filtering.WithAgencies([str(x) for x in agencies_selected])
        filter_mscales = filtering.WithMagnitudeScales([str(x) for x in mscales_selected])
        filter_mvalues = filtering.C(magnitude__gt=mag_range.low_value, magnitude__lt=mag_range.high_value)
        filter_dvalues = filtering.C(time__between=date_range)

        results = filter_agency & filter_mscales & filter_mvalues & filter_dvalues

        if selected_extent is not None:
            filter_pvalues = filtering.WithinPolygon(selected_extent.asWktPolygon())
            results = results & filter_pvalues

        self.create_layer(results)

    def create_layer(self, data):
        dock = self.dock
        date_range = ":".join([to_year(dock.minDateDe.dateTime()), to_year(dock.maxDateDe.dateTime())])
        mag_range = ":".join([str(dock.mag_range.lowValue()), str(dock.mag_range.highValue())])
        agencies = ",".join(map(str, dock.agenciesWidget.get_selected_items()))
        mscales = ",".join(map(str, dock.magnitudesWidget.get_selected_items()))

        display_name = "Events-%s-%s-%s-%s" % (date_range, mag_range, mscales, agencies)

        uri = "Point?crs=epsg:4326&index=yes&uuid=%s" % uuid.uuid4()
        vlayer = QgsVectorLayer(uri, display_name, "memory")
        QgsMapLayerRegistry.instance().addMapLayer(vlayer)

        provider = vlayer.dataProvider()
        vlayer.startEditing()
        provider.addAttributes(
            [
                QgsField("agency", QVariant.String),
                QgsField("event_name", QVariant.String),
                QgsField("event_measure", QVariant.String),
            ]
        )
        features = []
        for i, row in enumerate(data):
            x, y = row.position_as_tuple()
            feat = QgsFeature()
            geom = QgsGeometry.fromPoint(QgsPoint(x, y))
            feat.setGeometry(geom)
            feat.setAttributes([str(row.agency), row.event_name, str(row)])
            features.append(feat)
        provider.addFeatures(features)
        vlayer.commitChanges()
        vlayer.updateExtents()
        self.iface.mapCanvas().setExtent(vlayer.extent())
        vlayer.triggerRepaint()

    def load_countries(self):
        display_name = "Population density"
        uri = os.path.join(self.data_dir, "Countries.shp")
        vlayer = QgsVectorLayer(uri, display_name, "ogr")
        QgsMapLayerRegistry.instance().addMapLayers([vlayer])
Пример #7
0
    def populate_agencies(self):
	dbfile = '/home/michele/pippo.db'
        db = CatalogueDatabase(filename=dbfile)
        agencies = [str(a) for a in db.get_agencies()]
	self.dock.agenciesCombo.addItems(agencies)