def test_function_call(self): e = WKBElement(b'\x01\x02', extended=True) f = e.ST_Buffer(2) eq_sql(f, 'ST_Buffer(' 'ST_GeomFromEWKB(:ST_GeomFromEWKB_1), ' ':ST_Buffer_1)') assert f.compile().params == { u'ST_Buffer_1': 2, u'ST_GeomFromEWKB_1': b'\x01\x02', }
def test_function_call(self): e = WKBElement(b'\x01\x02') f = e.ST_Buffer(2) eq_sql(f, 'ST_Buffer(' 'ST_GeomFromWKB(:ST_GeomFromWKB_1, :ST_GeomFromWKB_2), ' ':ST_Buffer_1)') assert f.compile().params == { u'ST_Buffer_1': 2, u'ST_GeomFromWKB_1': b'\x01\x02', u'ST_GeomFromWKB_2': -1 }
def test_neq_other_types(self): a = WKBElement(self._ewkb, extended=True) b = WKBElement(self._wkb, srid=self._srid) c = WKTElement(self._wkt, srid=self._srid) d = WKTElement(self._ewkt, extended=True) e = WKBElement(self._hex, extended=True) all_elements = [a, b, c, d, None, 1, "test"] for i, j in permutations(all_elements, 2): assert i != j for i in all_elements[1:]: assert i != e and e != i
def test_eq(self): a = WKBElement(self._ewkb, extended=True) b = WKBElement(self._wkb, srid=self._srid) c = WKTElement(self._wkt, srid=self._srid) d = WKTElement(self._ewkt, extended=True) e = WKBElement(self._hex, extended=True) assert a == a assert b == b assert c == c assert d == d assert e == e assert a == e and e == a
def test_function_call(self): from geoalchemy2.elements import WKBElement e = WKBElement(b'\x01\x02') f = e.ST_Buffer(2) eq_sql( f, 'ST_Buffer(' 'ST_GeomFromWKB(:ST_GeomFromWKB_1, :ST_GeomFromWKB_2), ' ':param_1)') eq_( f.compile().params, { u'param_1': 2, u'ST_GeomFromWKB_1': b'\x01\x02', u'ST_GeomFromWKB_2': -1 })
def withinDistance2D(cls, bbox, fromSrid=4326, toSrid=4326, tolerance=0.): bboxGeom = shapelyBBox(bbox) wkbGeometry = WKBElement(buffer(bboxGeom.wkb), fromSrid) if fromSrid != toSrid: wkbGeometry = func.ST_Transform(wkbGeometry, toSrid) geomColumn = cls.geometryColumn() return func.ST_DWithin(geomColumn, wkbGeometry, tolerance)
def registerInDatabase(self, df): self.setGeoInfo(df) self.dropTable() if self.table == None: cols = self.columnsFromDataframe(df) self.createTable(cols) if self.inbulk: bulk = [] for k, row in df.iterrows(): # possibly modify "geom" and "rast" entries so they can be consumed by the database if self.geoinfo.rastname in row: row[self.geoinfo.rastname] = func.ST_FromGDALRaster( bytes(row[self.geoinfo.rastname]), srid=self.geoinfo.srid) if self.geoinfo.geoname in row: row[self.geoinfo.geoname] = WKBElement(shapely.wkb.dumps( row[self.geoinfo.geoname]), srid=self.geoinfo.srid) # slurplog.info(f"Adding entry {k}") if self.inbulk: bulk.append(row) else: self.addEntry(row) if self.inbulk: self.bulkInsert(bulk)
def test_to_shape_WKBElement_str(): # POINT(1 2) e = WKBElement(str('0101000000000000000000f03f0000000000000040')) s = to_shape(e) assert isinstance(s, Point) assert s.x == 1 assert s.y == 2
def geom_filter(cls, geometry, geometryType, imageDisplay, mapExtent, tolerance): toleranceMeters = getToleranceMeters(imageDisplay, mapExtent, tolerance) scale = None resolution = None minScale = cls.__minscale__ if hasattr(cls, '__minscale__') else None maxScale = cls.__maxscale__ if hasattr(cls, '__maxscale__') else None minResolution = cls.__minresolution__ if hasattr( cls, '__minresolution__') else None maxResolution = cls.__maxresolution__ if hasattr( cls, '__maxresolution__') else None if None not in (minScale, maxScale) and all( val != 0.0 for val in imageDisplay) and mapExtent.area != 0.0: scale = getScale(imageDisplay, mapExtent) if None not in (minResolution, maxResolution) and all( val != 0.0 for val in imageDisplay) and mapExtent.area != 0.0: resolution = getResolution(imageDisplay, mapExtent) if (scale is None or (scale > cls.__minscale__ and scale <= cls.__maxscale__)) and \ (resolution is None or (resolution > cls.__minresolution__ and resolution <= cls.__maxresolution__)): geom = esriRest2Shapely(geometry, geometryType) wkbGeometry = WKBElement(buffer(geom.wkb), 21781) geomColumn = cls.geometry_column() geomFilter = func.ST_DWITHIN(geomColumn, wkbGeometry, toleranceMeters) return geomFilter return None
def _truncate_last_segment(self, segment, start_point, exact_len, current_length): # Function is called when last segment is too long. # Previous length is current_length - current_segment_length # We have to take only (exact_len - previous_len) first meters of current_segment_length # Detect "correct" orientation of segment query = self.session.query(func.st_equals(func.ST_StartPoint(segment.geom), start_point)) previous_length = current_length - segment.length take_only = exact_len - previous_length truncate_point = float(take_only) / float(segment.length) # Correct SRID: 32632 (UTM WGS84 32 North) if not query.first()[0]: shorter_line = self.session.query(func.st_asewkb(func.st_Line_Substring(segment.geom, 0, truncate_point)), func.ST_Length( cast(func.st_Line_Substring(segment.geom, 0, truncate_point), Geography))).first() else: shorter_line = self.session.query(func.st_asewkb(func.st_Line_Substring(segment.geom, 1 - truncate_point, 1)), func.ST_Length( cast(func.st_Line_Substring(segment.geom, 1 - truncate_point, 1), Geography))).first() segment_labels = [] for label in segment.labels: segment_labels.append(model.Label(label=label.label, label_rate=label.label_rate)) shorter_segment = model.Segment(name=segment.name, geom=WKBElement(shorter_line[0]), length=shorter_line[1], labels=segment_labels) return shorter_segment
def orsiMetaExtractor(uri): """extract table data from the files""" lookup = { "stf": "Subtropical front", "saf": "Subantarctic front", "pf": "Polar front", "saccf": "Southern Antarctic circumpolar current front", "sbdy": "Southern Boundary of the Antarctic circumpolar current" } abbr = os.path.basename(uri.url)[:-4] geofront = ogr.Geometry(ogr.wkbMultiLineString) frontsegment = ogr.Geometry(ogr.wkbLineString) recomm = re.compile("^%") with open(uri.url, 'r') as fid: for ln in fid: if recomm.search(ln): #everytime we encounter a % we need to start a new segment and possibly update the multilinestring if frontsegment.GetPointCount() > 1: geofront.AddGeometry(frontsegment) frontsegment = ogr.Geometry(ogr.wkbLineString) continue lonlat = ln.split() frontsegment.AddPoint(float(lonlat[0]), float(lonlat[1])) geofront.FlattenTo2D() meta = { "acronym": abbr, "name": lookup[abbr], "geom": WKBElement(geofront.ExportToIsoWkb(), srid=4326) } return meta
def test_to_shape_WKBElement(): e = WKBElement(b'\x01\x01\x00\x00\x00\x00\x00\x00\x00\x00' b'\x00\xf0?\x00\x00\x00\x00\x00\x00\x00@') s = to_shape(e) assert isinstance(s, Point) assert s.x == 1 assert s.y == 2
def _split_start_line(self, point, segment): # Find segment's point nearest to point nearest_point = self.session.query(func.st_closestpoint(segment.geom, point)).first() # Locate nearest point on segment. Function returns the position on segment as rate located_point = self.session.query(func.st_line_locate_point(segment.geom, nearest_point)).first() # Split segment. First half starts from 0 and ends at located_point first_half = self.session.query(func.st_asewkb(func.st_Line_Substring(segment.geom, 0, located_point)), func.ST_Distance_Sphere(func.st_Line_Substring(segment.geom, 0, located_point), nearest_point), func.ST_Length(cast(func.st_Line_Substring(segment.geom, 0, located_point), Geography))).first() # Split segment. Second half starts from located_point and ends at 1 second_half = self.session.query(func.st_asewkb(func.st_Line_Substring(segment.geom, located_point, 1)), func.ST_Distance_Sphere(func.st_Line_Substring(segment.geom, located_point, 1), nearest_point), func.ST_Length(cast(func.st_Line_Substring(segment.geom, located_point, 1), Geography))).first() # Create temporary segments (do not add these segments to session), with their own labels first_segment_labels = [] second_segment_labels = [] for label in segment.labels: first_segment_labels.append(model.Label(label=label.label, label_rate=label.label_rate)) second_segment_labels.append(model.Label(label=label.label, label_rate=label.label_rate)) first_segment = model.Segment(name=segment.name, geom=WKBElement(first_half[0]), length=first_half[2], labels=first_segment_labels) second_segment = model.Segment(name=segment.name, geom=WKBElement(second_half[0]), length=second_half[2], labels=second_segment_labels) # If located point is the same as start point, return only second half if located_point[0] == 0.0: return [second_segment] # If located point is the same as end point, return only first half if located_point[0] == 1.0: return [first_segment] # Else, return two splitted segments return [first_segment, second_segment]
def test_pickle_unpickle(self): import pickle e = WKBElement(b'\x01\x02', srid=3, extended=True) pickled = pickle.dumps(e) unpickled = pickle.loads(pickled) assert unpickled.srid == 3 assert unpickled.extended is True assert unpickled.data == b'\x01\x02'
def bboxIntersects(cls, bbox, fromSrid=4326, toSrid=4326): bboxGeom = shapelyBBox(bbox) wkbGeometry = WKBElement(buffer(bboxGeom.wkb), fromSrid) if fromSrid != toSrid: wkbGeometry = func.ST_Transform(wkbGeometry, toSrid) geomColumn = cls.geometryColumn() return and_(geomColumn.intersects(wkbGeometry), func.ST_Intersects(geomColumn, wkbGeometry))
def geom_filter(cls, geometry, imageDisplay, mapExtent, tolerance, srid): tolerance_meters = get_tolerance_meters(imageDisplay, mapExtent, tolerance) geom_column = cls.geometry_column() wkb_geometry = WKBElement(buffer(geometry.wkb), srid) return func.ST_DWITHIN( geom_column, transform_geometry(geom_column, wkb_geometry, srid), tolerance_meters)
def test_unpack_srid_from_bin(self): """ Unpack SRID from WKB struct (when it is not provided as arg) to ensure geometry result processor preserves query SRID. """ e = WKBElement(self._bin, extended=True) assert e.srid == self._srid assert wkb.loads(bytes_(e.data)).wkt == self._wkt
def acc_inv_query(longitude, latitude, distance, start_date, end_date, school): lat_min, lon_min, lat_max, lon_max = get_bounding_box(latitude, longitude, distance) baseX = lon_min; baseY = lat_min; distanceX = lon_max; distanceY = lat_max; pol_str = 'POLYGON(({0} {1},{0} {3},{2} {3},{2} {1},{0} {1}))'.format(baseX, baseY, distanceX, distanceY) school_point = WKBElement('POINT({0} {1})'.format(longitude, latitude), srid=4326) query_obj = db.session.query(Involved, AccidentMarker) \ .join(AccidentMarker, AccidentMarker.provider_and_id == Involved.provider_and_id) \ .filter(AccidentMarker.geom.intersects(pol_str)) \ .filter(Involved.injured_type == INJURED_TYPE_PEDESTRIAN) \ .filter(AccidentMarker.provider_and_id == Involved.provider_and_id) \ .filter(or_((AccidentMarker.provider_code == CONST.CBS_ACCIDENT_TYPE_1_CODE), (AccidentMarker.provider_code == CONST.CBS_ACCIDENT_TYPE_3_CODE))) \ .filter(AccidentMarker.created >= start_date) \ .filter(AccidentMarker.created < end_date) \ .filter(AccidentMarker.location_accuracy == LOCATION_ACCURACY_PRECISE_INT) \ .filter(AccidentMarker.yishuv_symbol != YISHUV_SYMBOL_NOT_EXIST) \ .filter(Involved.age_group.in_([1,2,3,4])) #ages 0-19 df = pd.read_sql_query(query_obj.with_labels().statement, query_obj.session.bind) if LOCATION_ACCURACY_PRECISE: location_accurate = 1 location_approx = '' else: location_accurate = 1 location_approx = 1 ui_url_map_only = ANYWAY_UI_FORMAT_MAP_ONLY.format(latitude=school['latitude'], longitude=school['longitude'], start_date=start_date.strftime(DATE_URL_FORMAT), end_date=end_date.strftime(DATE_URL_FORMAT), acc_type=SUBTYPE_ACCIDENT_WITH_PEDESTRIAN, location_accurate=location_accurate, location_approx=location_approx) ui_url_with_filters = ANYWAY_UI_FORMAT_WITH_FILTERS.format(latitude=school['latitude'], longitude=school['longitude'], start_date=start_date.strftime(DATE_URL_FORMAT), end_date=end_date.strftime(DATE_URL_FORMAT), acc_type=SUBTYPE_ACCIDENT_WITH_PEDESTRIAN, location_accurate=location_accurate, location_approx=location_approx) df['anyway_link'] = ui_url_map_only df['anyway_link_with_filters'] = ui_url_with_filters df['school_id'] = school['id'] df['school_name'] = school['school_name'] df['school_yishuv_symbol'] = school['yishuv_symbol'] df['school_yishuv_name'] = school['yishuv_name'] df['school_longitude'] = school['longitude'] df['school_latitude'] = school['latitude'] return df
def register(self): #currently deletes all entries in the table self.truncateTable() #open main index file and read zipdir=self.cacheDir()+"/"+self.typ+"_"+self.freq with open(os.path.join(zipdir,'filelist.txt'),'r') as fid: for ln in fid: lnspl=ln.split(";") lat=float(lnspl[1]) lon=float(lnspl[2]) id=int(lnspl[0]) slurplogger().info("Indexing %s"%(lnspl[3])) geoLoc=ogr.Geometry(ogr.wkbPoint) geoLoc.AddPoint(lon,lat) meta={ "id":id, "statname":lnspl[3], "countrycode":lnspl[4], "formerid":lnspl[5], "geom":WKBElement(geoLoc.ExportToWkb(),srid=4326,extended=True), } #also open data file data={"time":[],"sl":[]} tmin=datetime.max tmax=datetime.min with open(os.path.join(zipdir,'data',"%d.%sdata"%(id,self.typ))) as dfid: for dln in dfid: tyear,valmm,dum1,dum2=dln.split(";") dt=decyear2dt(float(tyear)) if self.freq == 'monthly': dstart,dend=dt2monthlyinterval(dt) else: #yearly dstart,dend=dt2yearlyinterval(dt) tmin=min(dt,tmin) tmax=max(dt,tmax) data["time"].append(dt.isoformat()) data["sl"].append(1e3*int(valmm)) #open documentation files with open(os.path.join(zipdir,'docu',"%d.txt"%(id))) as docid: data["doc"]=docid.readlines() #open auth file with open(os.path.join(zipdir,'docu',"%d_auth.txt"%(id))) as docid: data["auth"]=docid.readlines() meta['tstart']=tmin meta["tend"]=tmax meta["data"]=data self.addEntry(meta) self.updateInvent()
def order_by_distance(cls, geometry, geometryType, imageDisplay, mapExtent, tolerance): toleranceMeters = getToleranceMeters(imageDisplay, mapExtent, tolerance) if toleranceMeters <= 250.0: geom = esriRest2Shapely(geometry, geometryType) wkbGeometry = WKBElement(buffer(geom.wkb), 21781) geomColumn = cls.geometry_column() return func.ST_DISTANCE(geomColumn, wkbGeometry) return None
def watermaskRasterize(cls, bbox, width=256, height=256, srid=4326): geomColumn = cls.geometryColumn() bboxGeom = shapelyBBox(bbox) wkbGeometry = WKBElement(buffer(bboxGeom.wkb), srid) # ST_DumpValues(Raster, Band Number, True -> returns None # and False -> returns numerical vals) return func.ST_DumpValues( bgdi_watermask_rasterize( wkbGeometry, width, height, '.'.join( (cls.__table_args__['schema'], cls.__tablename__)), geomColumn.name), 1, False)
def test_to_shape_ExtendedWKBElement(): # SRID=3857;POINT(1 2 3) e = WKBElement(b'\x01\x01\x00\x00\xa0\x11\x0f\x00\x00\x00' b'\x00\x00\x00\x00\x00\xf0?\x00\x00\x00\x00' b'\x00\x00\x00@\x00\x00\x00\x00\x00\x00\x08@', extended=True) s = to_shape(e) assert isinstance(s, Point) assert s.x == 1 assert s.y == 2 assert s.z == 3
def order_by_distance(cls, geometry, geometryType, imageDisplay, mapExtent, tolerance, limit, srid): tolerance_meters = get_tolerance_meters(imageDisplay, mapExtent, tolerance) # If limit is equal to 1 we have to be accurate if tolerance_meters <= 250.0 or limit == 1: wkb_geometry = WKBElement(buffer(geometry.wkb), srid) geom_column = cls.geometry_column() return func.ST_DISTANCE( geom_column, transform_geometry(geom_column, wkb_geometry, srid)) return None
def test_from_shape(): # Standard case: POINT(1 2) expected = WKBElement(str('0101000000000000000000f03f0000000000000040')) p = Point(1, 2) e = from_shape(p) assert isinstance(e, WKBElement) assert isinstance(e.data, buffer) assert e == expected s = shapely.wkb.loads(bytes(e.data)) assert isinstance(s, Point) assert s.equals(p) # Standard case with SRID: SRID=2145;POINT(1 2) expected2 = WKBElement(str('0101000000000000000000f03f0000000000000040'), srid=2154) p = Point(1, 2) e2 = from_shape(p, srid=2154) assert isinstance(e2, WKBElement) assert isinstance(e2.data, buffer) assert e2 == expected2 s2 = shapely.wkb.loads(bytes(e2.data)) assert isinstance(s2, Point) assert s2.equals(p) # Extended case: SRID=2145;POINT(1 2) expected3 = WKBElement( str('01010000206a080000000000000000f03f0000000000000040'), extended=True) e3 = from_shape(p, srid=2154, extended=True) assert isinstance(e3, WKBElement) assert isinstance(e3.data, buffer) assert e3 == expected3 s3 = shapely.wkb.loads(bytes(e3.data)) assert isinstance(s, Point) assert s3.equals(p)
def test_to_shape_WKBElement(): from geoalchemy2.elements import WKBElement try: from geoalchemy2.shape import to_shape from shapely.geometry import Point except ImportError: raise SkipTest e = WKBElement('\x01\x01\x00\x00\x00\x00\x00\x00\x00\x00' '\x00\xf0?\x00\x00\x00\x00\x00\x00\x00@') s = to_shape(e) ok_(isinstance(s, Point)) eq_(s.x, 1) eq_(s.y, 2)
def register(self, meshdir=None, abg=None): """register triangular elements in a FESOM mesh directory @param meshdir: directory where the mesh files reside""" if not meshdir: raise RuntimeError( "A meshdirectory needs to be supplied when registering this dataset" ) self.truncateTable() from pyfesom.load_mesh_data import fesom_mesh #load mesh from directory if abg: mesh = fesom_mesh(meshdir, abg=abg) else: mesh = fesom_mesh(meshdir) for i1, i2, i3 in mesh.elem: lons = mesh.x2[[i1, i2, i3]] # import ipdb;ipdb.set_trace() iscyclic = (lons.max() - lons.min()) > 100 ring = ogr.Geometry(ogr.wkbLinearRing) tri = ogr.Geometry(ogr.wkbPolygon) ring.AddPoint(lons[0], mesh.y2[i1]) ring.AddPoint(lons[1], mesh.y2[i2]) ring.AddPoint(lons[2], mesh.y2[i3]) #repeat the first point ring.AddPoint(lons[0], mesh.y2[i1]) tri.AddGeometry(ring) meta = { "nodeid": [int(i1), int(i2), int(i3)], "cyclic": iscyclic, "topo": (mesh.topo[i1] + mesh.topo[i2] + mesh.topo[i3]) / 3.0, "geom": WKBElement(tri.ExportToIsoWkb(), srid=4326, extended=True) } ring = None tri = None self.addEntry(meta) self._dbinvent.data[ "Description"] = "FESOM mesh surface triangles with 0-based node indices" self.setDataDir(os.path.abspath(meshdir)) # self._dbinvent.data["meshdir"]=os.path.abspath(meshdir) self.updateInvent()
def test_pickle_unpickle(self): import pickle e = WKBElement(self._bin, srid=self._srid, extended=True) pickled = pickle.dumps(e) unpickled = pickle.loads(pickled) assert unpickled.srid == self._srid assert unpickled.extended is True assert unpickled.data == bytes_(self._bin) assert unpickled.name == 'ST_GeomFromEWKB' f = unpickled.ST_Buffer(2) eq_sql(f, 'ST_Buffer(' 'ST_GeomFromEWKB(:ST_GeomFromEWKB_1), ' ':ST_Buffer_1)') assert f.compile().params == { u'ST_Buffer_1': 2, u'ST_GeomFromEWKB_1': bytes_(self._bin), }
def enhancetenv3Meta(meta, file): """Reorder some entries so that they agree with The only thing whch is currently indexed is that the uri is replaced by the filename,but more adavanced meta data extractions may be adaded here""" meta["uri"] = file meta["data"] = {} # adapt later to extract additional info # with gz.open(file,'rt') as fid: # slurplogger().info("Extracting info from %s"%(file)) # for cnt,ln in enumerate(fid): # pass #extract geographic point geoLoc = ogr.Geometry(ogr.wkbPoint) geoLoc.AddPoint(meta.pop("lon"), meta.pop("lat"), meta.pop("height")) meta["geom"] = WKBElement(geoLoc.ExportToWkb(), srid=4326, extended=True) return meta
def orasVerticesMetaExtract(datadir, datafile,cachedir=False): """A little generator which extracts rows from """ with Dataset(datadir+datafile, 'r') as nc_id: nav_lat=nc_id['nav_lat'][:] nav_lon=nc_id['nav_lon'][:] deptho_lev=nc_id['deptho_lev'][:] meta=[] for row in range(nc_id['nav_lat'][:].shape[0]): for column in range(nc_id['nav_lat'][:].shape[1]): # if mask[0,row,column]==1: point = ogr.Geometry(ogr.wkbPoint) point.AddPoint_2D(float(nav_lon[row,column]),float(nav_lat[row,column])) meta_entry=({"depthlevel":np.float(deptho_lev[row,column]), "geom":WKBElement(point.ExportToIsoWkb(),srid=4326,extended=True) }) meta.append(meta_entry) return meta
def register(self, meshdir=None, abg=None): """register vertices in a FESOM mesh directory @param meshdir: directory where the mesh files reside @param abg non-standard Euler angles, defaults to [50, 15, -90]""" #only load when needed from pyfesom.load_mesh_data import fesom_mesh if not meshdir: raise RuntimeError( "A meshdirectory needs to be supplied when registering this dataset" ) self.truncateTable() #load mesh from directory if abg: mesh = fesom_mesh(meshdir, abg=abg) else: mesh = fesom_mesh(meshdir) for id, (lon, lat, onbnd) in enumerate(zip(mesh.x2, mesh.y2, mesh.ind2d)): # create a point geometry point = ogr.Geometry(ogr.wkbPoint) point.AddPoint(lon, lat) meta = { "onboundary": int(onbnd), "topo": mesh.topo[id], "nodeid": [int(x - 1) for x in mesh.n32[id, :] if x >= 0], "geom": WKBElement(point.ExportToIsoWkb(), srid=4326, extended=True) } self.addEntry(meta) point = None self._dbinvent.data[ "Description"] = "FESOM mesh vertices with 0-based indecises for the surface layers" self.setDataDir(os.path.abspath(meshdir)) # self._dbinvent.data["meshdir"]=os.path.abspath(meshdir) self._dbinvent.data["zlevels"] = [zl for zl in mesh.zlevs] self.updateInvent()