def _make_pgsbox(coords): if len(coords)!=4: raise common.STCSParseError("STC-S boxes want four numbers.") x,y,w,h = coords return pgsphere.SPoly(( pgsphere.SPoint(x-w/2, y-h/2), pgsphere.SPoint(x-w/2, y+h/2), pgsphere.SPoint(x+w/2, y+h/2), pgsphere.SPoint(x+w/2, y-h/2)))
def _mogrifySpaceUnit(unit, nDim): if unit: parts = unit.split() if len(parts) == nDim: return tuple(parts) elif len(parts) == 1: return (unit, ) * nDim else: raise common.STCSParseError( "'%s' is not a valid for unit %d-dimensional" " spatial coordinates" % (unit, nDim))
def _addUnitRedshift(args, node, frame): unit = node.get("unit") if unit == "nil": args["unit"] = "" args["velTimeUnit"] = None elif unit: parts = unit.split("/") if len(parts) != 2: raise common.STCSParseError( "'%s' is not a valid unit for redshifts" % unit) args["unit"] = parts[0] args["velTimeUnit"] = parts[1]
def parse(s): if s is None or not s.strip(): # special service: Null values return None if isinstance(s, pgsphere.PgSAdapter): return s try: res = utils.pyparseString(region, s, parseAll=True)[0] if not res.cooSys or res.cooSys.lower()=='unknownframe': # Sigh. res.cooSys = "UNKNOWN" return res except (ParseException, ParseSyntaxException), msg: raise common.STCSParseError("Invalid STCS (%s)"%str(msg))
def _addUnitVelocity(args, node, frame): unit, nDim = node.get("unit"), frame.nDim if unit: su, vu = [], [] parts = unit.split() for uS in parts: up = uS.split("/") if len(up) != 2: raise common.STCSParseError( "'%s' is not a valid unit for velocities." % uS) su.append(up[0]) vu.append(up[1]) args["unit"] = _mogrifySpaceUnit(" ".join(su), nDim) args["velTimeUnit"] = _mogrifySpaceUnit(" ".join(vu), nDim)
def _validateCoos(values, nDim, minItems, maxItems): """makes sure values is valid a source of between minItems and maxItems nDim-dimensional tuples. minItems and maxItems may both be None to signify no limit. """ if isinstance(values, common.GeometryColRef): values.expectedLength = nDim numItems = len(values) / nDim if numItems * nDim != len(values): # special case: a *single* ColRef is good for anything (could be # an array or something) if len(values) == 1 and isinstance(values[0], common.ColRef): return raise common.STCSParseError( "%s is not valid input to create %d-dimensional" " coordinates" % (values, nDim)) if minItems is not None and numItems < minItems: raise common.STCSParseError("Expected at least %d coordinates in %s." % (minItems, values)) if maxItems is not None and numItems > maxItems: raise common.STCValueError( "Expected not more than %d coordinates in %s." % (maxItems, values))
def _makePgSphereInstance(match): """returns a utils.pgsphere instance from a match of simpleStatement in the simple STCS parser below. """ if match["flavor"] and match["flavor"].strip().upper()!="SPHERICAL2": raise common.STCSParseError("Only SPHERICAL2 STC-S supported here") refFrame = 'UnknownFrame' if match["frame"]: refFrame = match["frame"].strip() # refFrame gets thrown away here; to use it, we'd have to generate # ADQL nodes, and that would be clumsy for uploads. See rant above. handler = globals()["_make_pgs%s"%match["shape"].lower()] res = handler( tuple(float(s)*utils.DEG for s in match["coords"].strip().split() if s)) res.cooSys = refFrame return res
def _makeCooValues(nDim, values, minItems=None, maxItems=None, spatial=False): """returns a list of nDim-Tuples made up of values. If values does not contain an integral multiple of nDim items, the function will raise an STCSParseError. You can also optionally give a minimally or maximally expected number of tuples. If the constraints are violated, again an STCSParseError is raised. If spatial is true, tuples will be returned even for 1D data. """ if values is None: if minItems: raise common.STCSParseError( "Expected at least %s coordinate items but" " found none." % minItems) else: return _validateCoos(values, nDim, minItems, maxItems) return tuple(v for v in iterVectors(values, nDim, spatial))
def getCST(literal, grammarFactory=None): """returns a CST for an STC-S expression. grammarFactory is a function returning the grammar, in this case either getGrammar (which gets used if the argument is left out) or getColrefGrammar. """ # special case: the empty input yields an empty CST if not literal.strip(): return {} if grammarFactory is None: grammarFactory = getGrammar try: tree = makeTree( utils.pyparseString(grammarFactory()["stcsPhrase"], literal)) except (ParseException, ParseSyntaxException), ex: raise common.STCSParseError("Invalid STCS expression (%s at %s)" % (ex.msg, ex.loc), expr=literal, pos=ex.loc)
def _make_pgspolygon(coords): if len(coords)<6 or len(coords)%2: raise common.STCSParseError( "STC-S polygons want at least three number pairs") return pgsphere.SPoly( [pgsphere.SPoint(*p) for p in utils.iterConsecutivePairs(coords)])
def _make_pgscircle(coords): if len(coords)!=3: raise common.STCSParseError("STC-S circles want three numbers.") return pgsphere.SCircle(pgsphere.SPoint(*coords[:2]), coords[2])
def _make_pgsposition(coords): if len(coords)!=2: raise common.STCSParseError("STC-S points want two coordinates.") return pgsphere.SPoint(*coords)
def flatten(self): raise common.STCSParseError("Cannot serialize STC-S. Did you use" " Union or Intersection outside of CONTAINS or INTERSECTS?")
def _assertGrammar(cond, msg, pos): if not cond: raise common.STCSParseError(msg, pos)