Exemplo n.º 1
0
    def test_parse_simple_fault_src(self):

        expected = (
            {'table': 'pshai.mfd_evd', 'data': {
                'max_val': 6.9500000000000002,
                'total_cumulative_rate': 1.8988435199999998e-05,
                'min_val': 6.5499999999999998,
                'bin_size': 0.10000000000000009,
                'mfd_values': [
                    0.0010614989, 0.00088291626999999998,
                    0.00073437776999999999, 0.00061082879999999995,
                    0.00050806530000000003],
                'total_moment_rate': 281889786038447.25,
                'owner_id': None}},
            {'table': 'pshai.simple_fault', 'data': {
                'name': u'Mount Diablo Thrust',
                'upper_depth': 8.0,
                'mgf_evd_id': None,
                'mfd_tgr_id': None,
                'outline': \
                    geoalchemy.WKTSpatialElement(SIMPLE_FAULT_OUTLINE_WKT),
                'edge': geoalchemy.WKTSpatialElement(SIMPLE_FAULT_EDGE_WKT),
                'lower_depth': 13.0,
                'gid': u'src01',
                'owner_id': None,
                'dip': 38.0,
                'description': None}},
            {'table': 'pshai.source', 'data': {
                'r_depth_distr_id': None,
                'name': u'Mount Diablo Thrust',
                'tectonic_region': 'active',
                'rake': 90.0,
                'si_type': 'simple',
                'gid': u'src01',
                'simple_fault_id': None,
                'owner_id': None,
                'hypocentral_depth': None,
                'description': None,
                'input_id': None}})

        simple_data = db_loader.parse_simple_fault_src(self.simple)

        # The WKTSpatialElement objects do not make for nice comparisons.
        # So instead, we'll just test the text element of each object
        # to make sure the coords and spatial reference system match.
        # To do that, we need to remove the WKTSpatialElements so
        # we can compare the rest of the dicts for equality.
        exp_outline = expected[1]['data'].pop('outline')
        actual_outline = simple_data[1]['data'].pop('outline')

        self.assertEqual(exp_outline.geom_wkt, actual_outline.geom_wkt)

        exp_edge = expected[1]['data'].pop('edge')
        actual_edge = simple_data[1]['data'].pop('edge')

        self.assertEqual(exp_edge.geom_wkt, actual_edge.geom_wkt)

        # Now we can test the rest of the data.
        for idx, exp in enumerate(expected):
            helpers.assertDictAlmostEqual(self, exp, simple_data[idx])
Exemplo n.º 2
0
 def setUp(self):
     'Prepare database'
     # Prepare people
     people = []
     personPacks = [
         ('test_person1', model.hashString('test_person1'), u'test_person1', '*****@*****.**'),
         ('test_person2', model.hashString('test_person2'), u'test_person2', '*****@*****.**'),
     ]
     for personPack in personPacks:
         person = Session.query(model.Person).filter_by(username=personPack[0]).first()
         if not person:
             person = model.Person(*personPack)
             Session.add(person)
         people.append(person)
     Session.commit()
     self.person1Key, self.person2Key = [x.key for x in people]
     # Prepare tags
     tags = []
     self.tagTexts = [
         u'tag with features that are public',
         u'tag with features that are private to person1',
         u'tag with features that are private to person2',
     ]
     for tagText in self.tagTexts:
         tag = Session.query(model.Tag).filter_by(text=tagText).first()
         if not tag:
             tag = model.Tag(tagText)
             Session.add(tag)
         tags.append(tag)
     Session.commit()
     tag1Public, tag1Private, tag2Private = tags
     # Prepare features
     features = []
     featurePacks = [
         (people[0].id, geoalchemy.WKTSpatialElement('POINT(6 10)'), model.scopePublic),
         (people[0].id, geoalchemy.WKTSpatialElement('LINESTRING(3 4,10 50,20 25)'), model.scopePrivate),
         (people[1].id, geoalchemy.WKTSpatialElement('POLYGON((1 1,5 1,5 5,1 5,1 1),(2 2,2 3,3 3,3 2,2 2))'), model.scopePrivate),
     ]
     for featurePack in featurePacks:
         feature = model.Feature()
         feature.owner_id, feature.geometry, feature.scope = featurePack
         Session.add(feature)
         features.append(feature)
     feature1Public, feature1Private, feature2Private = features
     feature1Public.tags = [tag1Public]
     feature1Private.tags = [tag1Private]
     feature2Private.tags = [tag2Private]
     Session.commit()
Exemplo n.º 3
0
    def build_simple_fault_insert(fault):
        """
        Build up the simple fault dict. See the documentation for
        :py:function:`parse_simple_fault_src` for more information.
        """
        simple_fault = db.SIMPLE_FAULT.copy()
        simple_fault['name'] = fault.getName()
        simple_fault['gid'] = fault.getID()
        simple_fault['dip'] = fault.getDip()
        simple_fault['upper_depth'] = fault.getSeismDepthUpp()
        simple_fault['lower_depth'] = fault.getSeismDepthLow()

        trace = fault.getTrace()

        # coords are ordered as lon/lat/depth
        point_str_3d = lambda pt: \
            ' '.join([
                str(pt.getLongitude()),
                str(pt.getLatitude()),
                str(pt.getDepth())])

        coord_list = lambda point_list: \
            ', '.join([point_str_3d(point) for point in point_list])

        trace_coords = coord_list(trace)

        simple_fault['edge'] = \
            geoalchemy.WKTSpatialElement(
                'SRID=4326;LINESTRING(%s)' % trace_coords)

        surface = get_fault_surface(fault)

        location_list = surface.getSurfacePerimeterLocsList()

        formatter = java.jclass("LocationListFormatter")(location_list)

        outline_coords = formatter.format()

        simple_fault['outline'] = \
            geoalchemy.WKTSpatialElement(
                'SRID=4326;POLYGON((%s))' % outline_coords)

        simple_fault_insert = {
            'table': '%s.simple_fault' % db.PSHAI_TS,
            'data': simple_fault
        }

        return simple_fault_insert
Exemplo n.º 4
0
 def setLocation(self, xy, srid=4167):
     wkt = Database.scalar(
         'select astext(st_transform(st_setsrid(st_point(:x,:y),:srid),4167))',
         x=xy[0],
         y=xy[1],
         srid=srid)
     self.ref_point = ga.WKTSpatialElement(wkt)
Exemplo n.º 5
0
    def _write_to_db(self, csv_reader):
        """
            :param csv_reader: DictReader instance
            :type csv_reader: DictReader object `csv.DictReader`
        """

        mags = [
            'mb_val', 'mb_val_error', 'ml_val', 'ml_val_error', 'ms_val',
            'ms_val_error', 'mw_val', 'mw_val_error'
        ]

        for row in csv_reader:

            timestamp = self._date_to_timestamp(int(row['year']),
                                                int(row['month']),
                                                int(row['day']),
                                                int(row['hour']),
                                                int(row['minute']),
                                                int(row['second']))

            surface = self.soup.surface(semi_minor=row['semi_minor'],
                                        semi_major=row['semi_major'],
                                        strike=row['strike'])

            for mag in mags:
                row[mag] = row[mag].strip()

                # if m*val* are empty or a series of blank spaces, we assume
                # that the val is -999 for convention (ask Graeme if we want to
                # change this)
                if len(row[mag]) == 0:
                    row[mag] = None
                else:
                    row[mag] = float(row[mag])

            magnitude = self.soup.magnitude(mb_val=row['mb_val'],
                                            mb_val_error=row['mb_val_error'],
                                            ml_val=row['ml_val'],
                                            ml_val_error=row['ml_val_error'],
                                            ms_val=row['ms_val'],
                                            ms_val_error=row['ms_val_error'],
                                            mw_val=row['mw_val'],
                                            mw_val_error=row['mw_val_error'])

            wkt = 'POINT(%s %s)' % (row['longitude'], row['latitude'])
            self.soup.catalog(owner_id=1,
                              time=timestamp,
                              surface=surface,
                              eventid=row['eventid'],
                              agency=row['agency'],
                              identifier=row['identifier'],
                              time_error=row['time_error'],
                              depth=row['depth'],
                              depth_error=row['depth_error'],
                              magnitude=magnitude,
                              point=geoalchemy.WKTSpatialElement(wkt, 4326))

        # commit results
        self.soup.commit()
Exemplo n.º 6
0
 def setUp(self):
     'Prepare database'
     # Prepare people
     people = []
     personPacks = [
         ('test_person1', model.hashString('test_person1'), u'test_person1',
          '*****@*****.**'),
         ('test_person2', model.hashString('test_person2'), u'test_person2',
          '*****@*****.**'),
     ]
     for personPack in personPacks:
         person = Session.query(
             model.Person).filter_by(username=personPack[0]).first()
         if not person:
             person = model.Person(*personPack)
             Session.add(person)
         people.append(person)
     Session.commit()
     self.person1Key, self.person2Key = [x.key for x in people]
     # Prepare features
     features = []
     featurePacks = [
         (people[0].id,
          geoalchemy.WKTSpatialElement('LINESTRING(3 4,10 50,20 25)')),
         (people[1].id,
          geoalchemy.WKTSpatialElement(
              'POLYGON((1 1,5 1,5 5,1 5,1 1),(2 2,2 3,3 3,3 2,2 2))')),
     ]
     for featurePack in featurePacks:
         feature = model.Feature()
         feature.owner_id = featurePack[0]
         feature.geometry = featurePack[1]
         Session.add(feature)
         features.append(feature)
     Session.commit()
     self.feature1ID, self.feature2ID = [x.id for x in features]
Exemplo n.º 7
0
    def load_from_ogr(self, ogrlayer, strdecode):
        source_osr = ogrlayer.GetSpatialRef()
        target_osr = osr.SpatialReference()
        target_osr.ImportFromEPSG(self.srs_id)

        transform = osr.CoordinateTransformation(source_osr, target_osr)

        feature = ogrlayer.GetNextFeature()
        fid = 0
        while feature:
            fid += 1
            geom = feature.GetGeometryRef()

            if geom.GetGeometryType() == ogr.wkbPoint:
                geom = ogr.ForceToMultiPoint(geom)
            elif geom.GetGeometryType() == ogr.wkbLineString:
                geom = ogr.ForceToMultiLineString(geom)
            elif geom.GetGeometryType() == ogr.wkbPolygon:
                geom = ogr.ForceToMultiPolygon(geom)

            geom.Transform(transform)

            fld_values = dict()
            for i in range(feature.GetFieldCount()):
                fld_type = feature.GetFieldDefnRef(i).GetType()
                fld_value = None
                if fld_type == ogr.OFTInteger:
                    fld_value = feature.GetFieldAsInteger(i)
                elif fld_type == ogr.OFTReal:
                    fld_value = feature.GetFieldAsDouble(i)
                elif fld_type == ogr.OFTString:
                    fld_value = strdecode(feature.GetFieldAsString(i))

                fld_values[self[feature.GetFieldDefnRef(
                    i).GetNameRef()].key] = fld_value

            obj = self.model(fid=fid,
                             geom=ga.WKTSpatialElement(str(geom), self.srs_id),
                             **fld_values)
            DBSession.add(obj)

            feature = ogrlayer.GetNextFeature()
Exemplo n.º 8
0
 def setUp(self):
     'Prepare database'
     # Prepare people
     people = []
     personPacks = [
         ('test_person1', model.hashString('test_person1'), u'test_person1',
          '*****@*****.**'),
         ('test_person2', model.hashString('test_person2'), u'test_person2',
          '*****@*****.**'),
     ]
     for personPack in personPacks:
         person = Session.query(
             model.Person).filter_by(username=personPack[0]).first()
         if not person:
             person = model.Person(*personPack)
             Session.add(person)
         people.append(person)
     Session.commit()
     self.person1Key, self.person2Key = [x.key for x in people]
     # Prepare tags
     tags = []
     self.tagTexts = [
         u'tag with features that are public',
         u'tag with features that are private to person1',
         u'tag with features that are private to person2',
     ]
     for tagText in self.tagTexts:
         tag = Session.query(model.Tag).filter_by(text=tagText).first()
         if not tag:
             tag = model.Tag(tagText)
             Session.add(tag)
         tags.append(tag)
     Session.commit()
     tag1Public, tag1Private, tag2Private = tags
     # Prepare features
     features = []
     featurePacks = [
         (people[0].id,
          geoalchemy.WKTSpatialElement(
              'MULTIPOINT (-90.2307590000000062 15.7834710000000005, 126.9779692000000040 37.5665350000000018, -0.1963060000000000 5.5557169999999996, -91.5219589999999954 14.8361560000000008)'
          ), model.scopePublic, {
              'description': 'Santa Eulalia; Seoul; Accra; Xela'
          }),
         (people[0].id,
          geoalchemy.WKTSpatialElement(
              'LINESTRING (-87.6297981999999962 41.8781135999999989, -84.3879823999999985 33.7489953999999983, -122.4194154999999995 37.7749294999999989)'
          ), model.scopePrivate, {
              'description': 'Chicago; Atlanta; San Francisco'
          }),
         (people[1].id,
          geoalchemy.WKTSpatialElement(
              'LINESTRING (-74.0059731 40.7143528, -90.5352778 14.6133333)'
          ), model.scopePrivate, {
              'passenger': u'Hélène'
          }),
     ]
     for featurePack in featurePacks:
         feature = model.Feature()
         feature.owner_id, feature.geometry, feature.scope, feature.properties = featurePack
         Session.add(feature)
         features.append(feature)
     feature1Public, feature1Private, feature2Private = features
     feature1Public.tags = [tag1Public]
     feature1Private.tags = [tag1Private]
     feature2Private.tags = [tag2Private]
     Session.commit()
     self.feature1ID = feature1Public.id
Exemplo n.º 9
0
     abort(
         400,
         'Could not parse simplified=%s as an integer' % simplified)
 # If simplification is desired,
 if simplified:
     # If a bounding box is specified,
     if bbox:
         # Convert bounding box to a metric coordinate system
         try:
             multiPoint = shapely.wkb.loads(
                 str(
                     Session.execute(
                         geoalchemy.functions.wkb(
                             geoalchemy.functions.transform(
                                 geoalchemy.WKTSpatialElement(
                                     'MULTIPOINT(%s %s, %s %s)' %
                                     (minY, minX, maxY, maxX),
                                     srid), 3857))).fetchone()[0]))
         except sa.exc.InternalError:
             Session.rollback()
             y1, x1, y2, x2 = -20037508.34, -20037508.34, 20037508.34, 20037508.34
         else:
             y1, x1 = multiPoint.geoms[0].coords[0]
             y2, x2 = multiPoint.geoms[1].coords[0]
         metersPerPixel = min(abs(x2 - x1), abs(y2 - y1)) / 256.
     # If a bounding box is not specified,
     else:
         metersPerPixel = 0.2
     # Define transformation in a metric coordinate system
     simplify = lambda x: sa.func.ST_SimplifyPreserveTopology(
         geoalchemy.functions.transform(x, 3857), metersPerPixel)
 # If simplification is not desired,
Exemplo n.º 10
0
    def __call__(self):
        tableinfo = TableInfo.from_layer(self.layer)
        tableinfo.setup_metadata(tablename=self.layer._tablename)
        table = tableinfo.table

        columns = [
            table.columns.id,
        ]
        where = []

        if self._geom:
            columns.append(table.columns.geom.label('geom'))

        if self._box:
            columns.extend((
                sa.func.st_xmin(sa.text('geom')).label('box_left'),
                sa.func.st_ymin(sa.text('geom')).label('box_bottom'),
                sa.func.st_xmax(sa.text('geom')).label('box_right'),
                sa.func.st_ymax(sa.text('geom')).label('box_top'),
            ))

        selected_fields = []
        for f in tableinfo.fields:
            if not self._fields or f.keyname in self._fields:
                columns.append(table.columns[f.key].label(f.keyname))
                selected_fields.append(f)

        if self._filter_by:
            for k, v in self._filter_by.iteritems():
                if k == 'id':
                    where.append(table.columns.id == v)
                else:
                    where.append(table.columns[tableinfo[k].key] == v)

        if self._like:
            l = []
            for f in tableinfo.fields:
                if f.datatype == FIELD_TYPE.STRING:
                    l.append(table.columns[f.key].ilike('%' + self._like +
                                                        '%'))

            where.append(sa.or_(*l))

        if self._intersects:
            geom = ga.WKTSpatialElement(self._intersects.wkt,
                                        self._intersects.srid)
            where.append(geom.intersects(table.columns.geom))

        class QueryFeatureSet(FeatureSet):
            fields = selected_fields
            layer = self.layer

            _geom = self._geom
            _box = self._box
            _limit = self._limit
            _offset = self._offset

            def __iter__(self):
                query = sql.select(
                    columns,
                    whereclause=sa.and_(*where),
                    limit=self._limit,
                    offset=self._offset,
                    order_by=table.columns.id,
                )
                rows = DBSession.connection().execute(query)
                for row in rows:
                    fdict = dict([(f.keyname, row[f.keyname])
                                  for f in selected_fields])
                    yield Feature(
                        layer=self.layer,
                        id=row.id,
                        fields=fdict,
                        geom=geom_from_wkb(str(row.geom.geom_wkb))
                        if self._geom else None,
                        box=box(row.box_left, row.box_bottom, row.box_right,
                                row.box_top) if self._box else None)

            @property
            def total_count(self):
                query = sql.select([
                    sa.func.count(table.columns.id),
                ],
                                   whereclause=sa.and_(*where))
                res = DBSession.connection().execute(query)
                for row in res:
                    return row[0]

        return QueryFeatureSet()
Exemplo n.º 11
0
                try:
                    featureID = int(featureID)
                except ValueError:
                    abort(
                        400, 'Could not parse featureID=%s as an integer' %
                        featureID)
                feature = featureByID[featureID]
            # If featureID is not specified, add it
            else:
                feature = model.Feature()
                feature.owner_id = personID
                Session.add(feature)
            # Set
            feature.properties = featureProperties
            feature.scope = model.scopePublic if public else model.scopePrivate
            feature.geometry = geoalchemy.WKTSpatialElement(
                featureGeometryWKT, srid)
            feature.tags = tags
            # Append
            features.append(feature)
        # Update timestamps for each tag
        for tag in tags:
            tag.updateTimestamp()
        # Commit
        Session.commit()
        # Return
        return '\n'.join(str(x.id) for x in features)

    def delete(self):
        'Delete features'
        # Authenticate via personID or key
        personID = h.getPersonIDViaKey()