def test_point_collection(self): dt1 = datetime.date(2007, 12, 5) p1 = Point() p1.time = dt1 p1.location = sPoint(-180, -90, 0) member1 = Member(value=5.4, unit='m', name='Sea Surface Height', description='sea height', standard='sea_surface_height') member2 = Member(value=8.1, unit='m', name='Wave Height', description='wave height', standard='wave_height') p1.add_member(member1) p1.add_member(member2) dt2 = datetime.date(2008, 2, 14) p2 = Point() p2.time = dt2 p2.location = sPoint(-120, 50, 10) member3 = Member(value=5.4, unit='m', name='Sea Surface Height', description='sea height', standard='sea_surface_height') member4 = Member(value=8.1, unit='m', name='Wave Height', description='wave height', standard='wave_height') p2.add_member(member3) p2.add_member(member4) pc = PointCollection(elements=[p1,p2]) pc.calculate_bounds() assert pc.size == 2 assert pc.time_range[0] == dt1 assert pc.time_range[-1] == dt2 assert pc.depth_range[0] == p1.location.z assert pc.depth_range[-1] == p2.location.z assert pc.upper_right().equals(sPoint(p2.location.x, p2.location.y)) assert pc.lower_left().equals(sPoint(p1.location.x, p1.location.y))
def test_trajectory(self): dt1 = datetime(2012, 4, 1, 0) p1 = Point() p1.time = dt1 p1.location = sPoint(-121, 49, 40) member1 = Member(value=random.uniform(30,40), unit='°C', name='Water Temperatire', description='water temperature', standard='sea_water_temperature') member2 = Member(value=random.uniform(80,100), unit='PSU', name='Salinity', description='salinity', standard='salinity') p1.add_member(member1) p1.add_member(member2) dt2 = datetime(2012, 4, 1, 1) p2 = Point() p2.time = dt2 p2.location = sPoint(-120, 50, 60) member3 = Member(value=random.uniform(30,40), unit='°C', name='Water Temperatire', description='water temperature', standard='sea_water_temperature') member4 = Member(value=random.uniform(80,100), unit='PSU', name='Salinity', description='salinity', standard='salinity') p2.add_member(member3) p2.add_member(member4) tr = Trajectory(elements=[p1,p2]) tr.calculate_bounds() assert len(tr.get_path()) == 2 assert tr.size == 2 assert tr.type == "Trajectory" assert tr.time_range[0] == dt1 assert tr.time_range[-1] == dt2 assert tr.depth_range[0] == p1.location.z assert tr.depth_range[-1] == p2.location.z assert tr.upper_right().equals(sPoint(p2.location.x, p2.location.y)) assert tr.lower_left().equals(sPoint(p1.location.x, p1.location.y))
def test_section(self): day = 1 hour = 0 sc = Section() dt = None # 10 profiles for x in xrange(0, 10): lat = random.randint(40, 44) lon = random.randint(-74, -70) loc = sPoint(lon, lat, 0) minute = 0 dt = datetime(2012, 4, day, hour, minute) hour += 1 prof = Profile() prof.location = loc prof.time = dt # Each with 20 depths for y in xrange(0, 20): p = Point() p.time = dt p.location = sPoint(loc.x, loc.y, y) m1 = Member(value=random.uniform(30, 40), unit='°C', name='Water Temperatire', description='water temperature', standard='sea_water_temperature') m2 = Member(value=random.uniform(80, 100), unit='PSU', name='Salinity', description='salinity', standard='salinity') p.add_member(m1) p.add_member(m2) prof.add_element(p) # Next depth is 2 minutes from now dt = dt + timedelta(minutes=2) sc.add_element(prof) sc.calculate_bounds() assert len(sc.get_path()) == 10 assert sc.size == 10 assert sc.point_size == 200 assert sc.type == "Section" assert len(sc.time_range) == 200 assert sc.time_range[0] == datetime(2012, 4, 1, 0, 0) assert sc.time_range[-1] == dt - timedelta(minutes=2) assert len(sc.depth_range) == 200 assert sc.depth_range[0] == 0 assert sc.depth_range[-1] == 19
def test_section_collection(self): s_collection = SectionCollection() # 20 sections for x in range(0,20): day = 1 hour = 0 sc = Section() dt = None # 10 profiles per section for x in range(0,10): lat = random.randint(40,44) lon = random.randint(-74,-70) loc = sPoint(lon,lat,0) minute = 0 dt = datetime(2012, 4, day, hour, minute) hour += 1 prof = Profile() prof.location = loc prof.time = dt # Each with 20 depths for y in range(0,20): p = Point() p.time = dt p.location = sPoint(loc.x, loc.y, y) m1 = Member(value=random.uniform(30,40), unit='°C', name='Water Temperature', description='water temperature', standard='sea_water_temperature') m2 = Member(value=random.uniform(80,100), unit='PSU', name='Salinity', description='salinity', standard='salinity') p.add_member(m1) p.add_member(m2) prof.add_element(p) # Next depth is 2 minutes from now dt = dt + timedelta(minutes=2) sc.add_element(prof) s_collection.add_element(sc) s_collection.calculate_bounds() assert s_collection.depth_range[0] == 0 assert s_collection.depth_range[-1] == 19 assert s_collection.time_range[0] == datetime(2012, 4, 1, 0) for section in s_collection: assert section.type == "Section" for profile in section: assert profile.type == "Profile" for point in profile: assert point.type == "Point" for point in s_collection.flatten(): assert point.type == "Point"
def test_section_collection(self): s_collection = SectionCollection() # 20 sections for x in xrange(0,20): day = 1 hour = 0 sc = Section() dt = None # 10 profiles per section for x in xrange(0,10): lat = random.randint(40,44) lon = random.randint(-74,-70) loc = sPoint(lon,lat,0) minute = 0 dt = datetime(2012, 4, day, hour, minute) hour += 1 prof = Profile() prof.location = loc prof.time = dt # Each with 20 depths for y in xrange(0,20): p = Point() p.time = dt p.location = sPoint(loc.x, loc.y, y) m1 = Member(value=random.uniform(30,40), unit='°C', name='Water Temperatire', description='water temperature', standard='sea_water_temperature') m2 = Member(value=random.uniform(80,100), unit='PSU', name='Salinity', description='salinity', standard='salinity') p.add_member(m1) p.add_member(m2) prof.add_element(p) # Next depth is 2 minutes from now dt = dt + timedelta(minutes=2) sc.add_element(prof) s_collection.add_element(sc) s_collection.calculate_bounds() assert s_collection.depth_range[0] == 0 assert s_collection.depth_range[-1] == 19 assert s_collection.time_range[0] == datetime(2012, 4, 1, 0) for section in s_collection: assert section.type == "Section" for profile in section: assert profile.type == "Profile" for point in profile: assert point.type == "Point" for point in s_collection.flatten(): assert point.type == "Point"
def __init__(self, **kwargs): super(SweTimeSeries,self).__init__(**kwargs) # Parse out GML point. Defaults to 0 depth if none specified self.geo_srs = Crs(testXMLAttribute(self._location.find(nsp("Point", self.GML_NS)), 'srsName')) geo = [float(v) for v in testXMLValue(self._location.find(nsp("Point/pos", self.GML_NS))).split(" ")] if self.geo_srs.axisorder == "yx": self.geo = sPoint(geo[1], geo[0]) else: self.geo = sPoint(geo[0], geo[1]) try: self.geo.z = geo[2] except: pass pc = PointCollection() for row in self.results.data: p = Point() time = None z = None lat = None lon = None for field in row: if field.axis == "time": t = dateutil.parser.parse(field.value) p.time = t.astimezone(dateutil.tz.tzutc()) elif field.axis == "Long": lon = field elif field.axis == "Lat": lat = field elif field.axis == "h": z = field else: m = Member(value=field.value, unit=field.units, units_definition=field.units_url, name=field.name, definition=field.definition, standard=field.definition) p.add_member(m) # Set the spatial point if lon.srs != lat.srs: raise ValueError("Longitude and Latitude need to have the same SRS/CRS!") p.location = sPoint(float(lon.value), float(lat.value), float(z.value)) pc.add_element(p) self.data = pc
def test_profile_collection(self): day = 1 pc = ProfileCollection() dt = None # 10 profiles for x in range(0,10): lat = random.randint(40,44) lon = random.randint(-74,-70) loc = sPoint(lon,lat,0) hour = 0 minute = 0 dt = datetime(2012, 4, day, hour, minute) prof = Profile() prof.location = loc prof.time = dt # Each with 20 depths for y in range(0,20): p = Point() p.time = dt p.location = sPoint(loc.x, loc.y, y) m1 = Member(value=random.uniform(30,40), unit='°C', name='Water Temperature', description='water temperature', standard='sea_water_temperature') m2 = Member(value=random.uniform(80,100), unit='PSU', name='Salinity', description='salinity', standard='salinity') p.add_member(m1) p.add_member(m2) prof.add_element(p) # Next depth is 2 minutes from now dt = dt + timedelta(minutes=2) pc.add_element(prof) pc.calculate_bounds() assert pc.size == 10 assert pc.point_size == 200 assert len(pc.time_range) == 200 assert pc.time_range[0] == datetime(2012, 4, 1, 0, 0) assert pc.time_range[-1] == dt - timedelta(minutes=2) assert len(pc.depth_range) == 200 assert pc.depth_range[0] == 0 assert pc.depth_range[-1] == 19 for profile in pc: assert profile.type == "Profile" for point in profile: assert point.type == "Point" for point in pc.flatten(): assert point.type == "Point"
def test_station_profile(self): sp = StationProfile() sp.name = "Profile Station" sp.location = sPoint(-77, 33) sp.uid = "1234" sp.set_property("authority", "IOOS") # add a sequence of profiles for y in range(3): dt1 = datetime(2013, 1, 1, 12, 0, 10 * y) prof1 = Profile() prof1.location = sPoint(-77, 33) prof1.time = dt1 # add a string of points going down in z for x in range(5): p1 = Point() p1.time = dt1 p1.location = sPoint(-77, 33, -5 * x) member1 = Member(value=30 - (2 * x), units='°C', name='Water Temperature', description='water temperature', standard='sea_water_temperature') member2 = Member(value=80 + (2 * x), units='PSU', name='Salinity', description='salinity', standard='salinity') p1.add_member(member1) p1.add_member(member2) prof1.add_element(p1) sp.add_element(prof1) sp.calculate_bounds() assert sp.size == 3 assert len(sp.time_range) == 3 assert sp.depth_range[0] == -20 assert sp.depth_range[-1] == 0 assert sp.get_property("authority") == "IOOS" assert sp.uid == "1234" assert len(sp.get_unique_members()) == 2
def _parse_location(self, loc_el, station_point): vector = loc_el.content srss = vector.referenceFrame.split("&") hsrs = None try: hsrs = Crs(srss[0]) except ValueError: pass vsrs = None try: vsrs = Crs(srss[-1].replace("2=http:", "http:")) except ValueError: pass local_frame = vector.localFrame lat = vector.get_by_name("latitude").content.value lon = vector.get_by_name("longitude").content.value z = vector.get_by_name("height").content.value loc = [lon, lat] if z: loc.append(z) else: loc.append(station_point.z) location = {'horizontal_srs' : hsrs, 'vertical_srs' : vsrs, 'localFrame' : local_frame, 'point' : sPoint(*loc)} return location
def _paths_to_cost(self): print "Analyzing paths..." count = 0 for path in self.paths: count += 1 print "\t%d of %d" % (count, len(self.paths)) distances = np.empty((self.height, self.width)) distances.fill(np.inf) min_x, min_y, max_x, max_y = path.bounds faraway = 5 min_x -= faraway min_y -= faraway max_x += faraway max_y += faraway print "\t\tPath bounding box area: %fm by %fm" % ( abs(max_x - min_x), abs(max_y - min_y)) for x in np.arange(min_x, max_x, self.resolution): for y in np.arange(min_y, max_y, self.resolution): i, j = self._world_to_map(x, y) pt = sPoint(x, y) distances[i, j] = path.distance(pt) self.cost_grid += np.exp(-(distances**2))
def test_trajectory(self): dt1 = datetime(2012, 4, 1, 0) p1 = Point() p1.time = dt1 p1.location = sPoint(-121, 49, 40) member1 = Member(value=random.uniform(30, 40), unit='°C', name='Water Temperatire', description='water temperature', standard='sea_water_temperature') member2 = Member(value=random.uniform(80, 100), unit='PSU', name='Salinity', description='salinity', standard='salinity') p1.add_member(member1) p1.add_member(member2) dt2 = datetime(2012, 4, 1, 1) p2 = Point() p2.time = dt2 p2.location = sPoint(-120, 50, 60) member3 = Member(value=random.uniform(30, 40), unit='°C', name='Water Temperatire', description='water temperature', standard='sea_water_temperature') member4 = Member(value=random.uniform(80, 100), unit='PSU', name='Salinity', description='salinity', standard='salinity') p2.add_member(member3) p2.add_member(member4) tr = Trajectory(elements=[p1, p2]) tr.calculate_bounds() assert len(tr.get_path()) == 2 assert tr.size == 2 assert tr.type == "Trajectory" assert tr.time_range[0] == dt1 assert tr.time_range[-1] == dt2 assert tr.depth_range[0] == p1.location.z assert tr.depth_range[-1] == p2.location.z assert tr.upper_right().equals(sPoint(p2.location.x, p2.location.y)) assert tr.lower_left().equals(sPoint(p1.location.x, p1.location.y))
def get_pwqmn(station_id, **kwargs): conn = sqlite3.connect(app.config.get("DATABASE")) with conn: conn.row_factory = sqlite3.Row cur = conn.cursor() cur.execute("SELECT * FROM stations WHERE STATION='%s'" % station_id) row = cur.fetchone() if row is not None: # Serve out OME s = pStation() s.uid = row["STATION"] s.name = row["NAME"] s.description = row["LOCATION"] s.location = sPoint(row["Longitude"], row["Latitude"], 0) s.set_property("country", "CA") s.set_property("organization_name", "Ontario Ministry of the Environment") s.set_property("organization_id", "ENE") filters = [] starting = kwargs.get("starting", None) ending = kwargs.get("ending", None) obs = kwargs.get("observedProperties", None) if starting is not None: filters.append("AND DATE > %s" % starting.strftime("%Y-%m-%dT%H:%M:%S")) if ending is not None: filters.append("AND DATE < %s" % ending.strftime("%Y-%m-%dT%H:%M:%S")) if obs is not None: obs = map(lambda x: "'%s'" % x, obs) filters.append("AND PARM in (%s)" % ",".join(obs)) cur.execute("SELECT * FROM data WHERE STATION='%s' %s ORDER BY DATE ASC" % (station_id, " ".join(filters))) rows = cur.fetchall() for d, members in itertools.groupby(rows, key=lambda s: s[3]): p = Point() p.time = datetime.strptime(d, "%Y-%m-%dT%H:%M:%S").replace(tzinfo=pytz.utc) for m in members: p.add_member( Member( value=m["RESULT"], unit=m["UNITS"], name=m["PARM"], description=m["PARM_DESCRIPTION"], standard=None, method_id=m["METHOD"], method_name=m["METHOD"], ) ) s.add_element(p) s.calculate_bounds() publisher = { "name": "Ontario Ministry of the Environment", "url": "http://www.ene.gov.on.ca/environment/en/resources/collection/data_downloads/index.htm#PWQMN", } return s, publisher return None, None
def test_trajectory_collection(self): t_collection = TrajectoryCollection() # 20 trajectories for x in range(0, 20): tr = Trajectory() month = random.randint(1, 12) dt = datetime(2012, month, 1, 0) # Starting point lat = random.randint(40, 44) lon = random.randint(-74, -70) depth = 0 # 100 points in each trajectory for l in range(0, 100): lat += random.uniform(-0.25, 0.25) lon += random.uniform(-0.25, 0.25) depth += random.randint(-4, 4) if depth < 0: depth = 0 p1 = Point() p1.location = sPoint(lon, lat, depth) dt += timedelta(hours=1) p1.time = dt member1 = Member(value=random.uniform(30, 40), unit='°C', name='Water Temperature', description='water temperature', standard='sea_water_temperature') member2 = Member(value=random.uniform(80, 100), unit='PSU', name='Salinity', description='salinity', standard='salinity') p1.add_member(member1) p1.add_member(member2) tr.add_element(p1) t_collection.add_element(tr) t_collection.calculate_bounds() assert len(t_collection.time_range) == 2000 assert len(t_collection.depth_range) == 2000 for trajectory in t_collection: assert trajectory.type == "Trajectory" for point in trajectory: assert point.type == "Point" for point in t_collection.flatten(): assert point.type == "Point"
def __init__(self, wqx_metadata, wqx_data): if not isinstance(wqx_metadata, WqxOutbound): wqx_metadata = WqxOutbound(wqx_metadata) if not isinstance(wqx_data, WqxOutbound): wqx_data = WqxOutbound(wqx_data) if wqx_data.failed or wqx_metadata.failed: self.feature = None else: stations = [] station_lookup = [] # Create a station for every MonitoringLocation for org in wqx_metadata.organizations: for ml in org.locations: s = Station() s.uid = ml.id s.name = ml.name s.set_property("station_type", ml.type) s.set_property("location_description", ml.description) s.set_property("huc", ml.huc) s.set_property("county", ml.county) s.set_property("state", ml.state) s.set_property("country", ml.country) s.set_property("organization_id", org.description.id) s.set_property("organization_name", org.description.name) s.set_property("vertical_units", ml.vertical_measure_units) s.set_property("horizontal_crs", ml.horizontal_crs_name) s.set_property("vertical_crs", ml.vertical_crs_name) # Now set the station's location vertical = 0 try: vertical = float(ml.vertical_measure_value) except: pass # convert the vertical to meters if it is ft (which it always is) if ml.vertical_measure_units == "ft": vertical /= 3.28084 s.set_property("vertical_units", "m") s.location = sPoint(float(ml.longitude), float(ml.latitude), vertical) stations.append(s) station_lookup.append(s.uid) for org in wqx_data.organizations: for a in org.activities: p = Point() p.time = a.start_time for r in a.results: p.add_member(Member(value=r.value, unit=r.units, name=r.name, description=r.short_name, standard=None, quality=r.quality, method_id=a.method_id, method_name=a.method_name)) # Assign data to the correct station pulled from the metadata station = stations[station_lookup.index(a.location_id)] p.location = station.location station.add_element(p) self.feature = StationCollection(elements=stations)
def point_to_node(pt): ''' converts a Steiner point (which by definition has node=None) into a Branch node. Make sure we do this only once as we want to use that node in a graph later on. This is as simple as just assigning the node property to the generated Branch ''' if pt.node is None: pt.node = types.Branch(sPoint(pt.x, pt.y), net=net) return pt.node
def __init__(self, response_list, nerrs_stations=None): assert isinstance(response_list, dict) if nerrs_stations is None: from pyoos.collectors.nerrs.nerrs_soap import NerrsSoap nerrs_stations = NerrsSoap().stations def get_station(feature): for s in nerrs_stations: if s['Station_Code'].lower() == feature.lower(): return s skip_tags = ["DateTimeStamp", "utcStamp", "data", "MaxWSpdT"] stations = [] for feature, response in response_list.iteritems(): if not isinstance(response, etree._Element): response = etree.fromstring(response) feature = get_station(feature) s = Station() s.uid = feature['Station_Code'] s.name = feature['Station_Name'] s.location = sPoint(float(feature['Longitude']), float(feature['Latitude']), 0) s.set_property("state", feature['State']) s.set_property("siteid", feature['NERR_Site_ID']) s.set_property("horizontal_crs", "EPSG:4326") s.set_property("vertical_units", "m") s.set_property("vertical_crs", "EPSG:4297") s.set_property("location_description", feature['Reserve_Name']) for data in response.findall(".//data"): p = Point() t = AsaTime.parse(testXMLValue(data.find("utcStamp"))) t = t.replace(tzinfo=pytz.utc) p.time = t p.location = s.location for child in data: if child.tag not in skip_tags: try: val = float(child.text) p.add_member( Member(value=val, unit=units(child.tag), name=child.tag, description=child.tag, standard=standard(child.tag))) except TypeError: # Value was None pass s.add_element(p) stations.append(s) self.feature = StationCollection(elements=stations)
def test_station(self): dt1 = datetime(2012, 1, 1, 12, 0) p1 = Point() p1.time = dt1 member1 = Member(value=34.7, unit='°C', name='Water Temperature', description='water temperature', standard='sea_water_temperature') member2 = Member(value=80, unit='PSU', name='Salinity', description='salinity', standard='salinity') p1.add_member(member1) p1.add_member(member2) dt2 = datetime(2012, 1, 1, 12, 10) p2 = Point() p2.time = dt2 member3 = Member(value=34.1, unit='°C', name='Water Temperature', description='water temperature', standard='sea_water_temperature') member4 = Member(value=70, unit='PSU', name='Salinity', description='salinity', standard='salinity') p2.add_member(member3) p2.add_member(member4) dt3 = datetime(2012, 1, 1, 12, 20) p3 = Point() p3.time = dt3 member5 = Member(value=32.6, unit='°C', name='Water Temperature', description='water temperature', standard='sea_water_temperature') member6 = Member(value=60, unit='PSU', name='Salinity', description='salinity', standard='salinity') member6 = Member(value=112, unit='%', name='DO', description='do', standard='do') p3.add_member(member5) p3.add_member(member6) pc = Station(elements=[p1,p2,p3]) pc.name = "Super Station" pc.location = sPoint(-120, 50, 0) pc.location_name = "Just south of the super pier" pc.uid = "123097SDFJL2" pc.set_property("authority", "IOOS") pc.calculate_bounds() assert pc.size == 3 assert len(pc.time_range) == 3 assert pc.time_range[0] == dt1 assert pc.time_range[-1] == dt3 assert len(pc.depth_range) == 3 assert pc.depth_range[0] == p1.location.z assert pc.upper_right().equals(pc.location) assert pc.lower_left().equals(pc.location) assert pc.get_property("authority") == "IOOS" assert pc.uid == "123097SDFJL2" assert pc.location_name == "Just south of the super pier" assert len(pc.get_unique_members()) == 3 filtered_elements = pc.filter_by_time(starting=dt1, ending=dt2) assert len(list(filtered_elements)) == 2 filtered_variables = pc.filter_by_variable("sea_water_temperature") assert len(list(filtered_variables)) == 3 filtered_variables = pc.filter_by_variable("do") assert len(list(filtered_variables)) == 1
def test_profile(self): dt1 = datetime(2012, 1, 1, 12, 0) p1 = Point() p1.time = dt1 p1.location = sPoint(-120, 50, 0) member1 = Member(value=34.7, unit='°C', name='Water Temperatire', description='water temperature', standard='sea_water_temperature') member2 = Member(value=80, unit='PSU', name='Salinity', description='salinity', standard='salinity') p1.add_member(member1) p1.add_member(member2) dt2 = datetime(2012, 1, 1, 12, 10) p2 = Point() p2.time = dt2 p2.location = sPoint(-120, 50, 10) member3 = Member(value=34.1, unit='°C', name='Water Temperatire', description='water temperature', standard='sea_water_temperature') member4 = Member(value=70, unit='PSU', name='Salinity', description='salinity', standard='salinity') p2.add_member(member3) p2.add_member(member4) dt3 = datetime(2012, 1, 1, 12, 20) p3 = Point() p3.time = dt3 p3.location = sPoint(-120, 50, 20) member5 = Member(value=32.6, unit='°C', name='Water Temperatire', description='water temperature', standard='sea_water_temperature') member6 = Member(value=60, unit='PSU', name='Salinity', description='salinity', standard='salinity') p3.add_member(member5) p3.add_member(member6) pc = Profile(elements=[p1,p2,p3]) pc.location = sPoint(-120, 50) pc.time = dt1 pc.calculate_bounds() assert pc.size == 3 assert pc.time == dt1 assert len(pc.time_range) == 3 assert pc.time_range[0] == dt1 assert pc.time_range[-1] == dt3 assert len(pc.depth_range) == 3 assert pc.depth_range[0] == p1.location.z assert pc.depth_range[-1] == p3.location.z assert pc.upper_right().equals(pc.location) assert pc.lower_left().equals(pc.location)
def test_station_profile(self): sp = StationProfile() sp.name = "Profile Station" sp.location = sPoint(-77, 33) sp.uid = "1234" sp.set_property("authority", "IOOS") # add a sequence of profiles for y in xrange(3): dt1 = datetime(2013, 1, 1, 12, 0, 10 * y) prof1 = Profile() prof1.location = sPoint(-77, 33) prof1.time = dt1 # add a string of points going down in z for x in xrange(5): p1 = Point() p1.time = dt1 p1.location = sPoint(-77, 33, -5 * x) member1 = Member(value=30 - (2 * x), units='°C', name='Water Temperature', description='water temperature', standard='sea_water_temperature') member2 = Member(value=80 + (2 * x), units='PSU', name='Salinity', description='salinity', standard='salinity') p1.add_member(member1) p1.add_member(member2) prof1.add_element(p1) sp.add_element(prof1) sp.calculate_bounds() assert sp.size == 3 assert len(sp.time_range) == 3 assert sp.depth_range[0] == -20 assert sp.depth_range[-1] == 0 assert sp.get_property("authority") == "IOOS" assert sp.uid == "1234" assert len(sp.get_unique_members()) == 2
def test_point(self): dt = datetime.utcnow() p = Point() p.location = sPoint(-123.17, 48.33, 10) p.time = dt assert p.location.x == -123.17 assert p.location.y == 48.33 assert p.location.z == 10 assert p.time == dt assert p.type == "Point"
def __init__(self, response_list, nerrs_stations=None): assert isinstance(response_list, dict) if nerrs_stations is None: from pyoos.collectors.nerrs.nerrs_soap import NerrsSoap nerrs_stations = NerrsSoap().stations def get_station(feature): for s in nerrs_stations: if s['Station_Code'].lower() == feature.lower(): return s skip_tags = ["DateTimeStamp", "utcStamp", "data", "MaxWSpdT"] stations = [] for feature, response in response_list.items(): if not isinstance(response, etree._Element): response = etree.fromstring(response) feature = get_station(feature) s = Station() s.uid = feature['Station_Code'] s.name = feature['Station_Name'] s.location = sPoint(feature['Longitude'], feature['Latitude'], 0) s.set_property("state", feature['State']) s.set_property("siteid", feature['NERR_Site_ID']) s.set_property("horizontal_crs", "EPSG:4326") s.set_property("vertical_units", "m") s.set_property("vertical_crs", "EPSG:4297") s.set_property("location_description", feature['Reserve_Name']) for data in response.findall(".//data"): p = Point() t = AsaTime.parse(testXMLValue(data.find("utcStamp"))) t = t.replace(tzinfo=pytz.utc) p.time = t p.location = s.location for child in data: if child.tag not in skip_tags: try: val = float(child.text) p.add_member(Member(value=val, name=child.tag, description=child.tag, unit=units(child.tag), standard=standard(child.tag))) except TypeError: # Value was None pass s.add_element(p) stations.append(s) self.feature = StationCollection(elements=stations)
def test_trajectory_collection(self): t_collection = TrajectoryCollection() # 20 trajectories for x in xrange(0,20): tr = Trajectory() month = random.randint(1,12) dt = datetime(2012, month, 1, 0) # Starting point lat = random.randint(40,44) lon = random.randint(-74,-70) depth = 0 # 100 points in each trajectory for l in xrange(0,100): lat += random.uniform(-0.25,0.25) lon += random.uniform(-0.25,0.25) depth += random.randint(-4,4) if depth < 0: depth = 0 p1 = Point() p1.location = sPoint(lon,lat,depth) dt += timedelta(hours=1) p1.time = dt member1 = Member(value=random.uniform(30,40), unit='°C', name='Water Temperature', description='water temperature', standard='sea_water_temperature') member2 = Member(value=random.uniform(80,100), unit='PSU', name='Salinity', description='salinity', standard='salinity') p1.add_member(member1) p1.add_member(member2) tr.add_element(p1) t_collection.add_element(tr) t_collection.calculate_bounds() assert len(t_collection.time_range) == 2000 assert len(t_collection.depth_range) == 2000 for trajectory in t_collection: assert trajectory.type == "Trajectory" for point in trajectory: assert point.type == "Point" for point in t_collection.flatten(): assert point.type == "Point"
def _build_obs_point(self, sensor_info, obs_recs): """ Pulls bin/profile height info out and calcs a z. TODO: currently extremely naive Returns a 2-tuple: point, remaining obs_recs """ cur_point = sensor_info['location']['point'] keys = [m['name'] for m in obs_recs] if 'binIndex' in keys: zidx = keys.index('binIndex') bin_index = int(obs_recs[zidx]['value']) z = sensor_info['profile_heights']['values'][bin_index] point = sPoint(cur_point.x, cur_point.y, cur_point.z + z) elif 'profileIndex' in keys: zidx = keys.index('profileIndex') bin_index = int(obs_recs[zidx]['value']) # @TODO take into account orientation, may change x/y/z # @TODO bin edges? z = sensor_info['profile_bins']['bin_center']['values'][bin_index] point = sPoint(cur_point.x, cur_point.y, cur_point.z + z) else: raise ValueError("no binIndex or profileIndex in Member: %s", keys) # remove z related Member obs_recs = obs_recs[:] obs_recs.pop(zidx) return point, obs_recs
def _get_point(self, profile, point): """ Finds the given point in the profile, or adds it in sorted z order. """ cur_points_z = [p.location.z for p in profile.elements] try: cur_idx = cur_points_z.index(point.z) return profile.elements[cur_idx] except ValueError: new_idx = bisect_left(cur_points_z, point.z) new_point = Point() new_point.location = sPoint(point) new_point.time = profile.time profile.elements.insert(new_idx, new_point) return new_point
def __init__(self, wqx_metadata, wqx_data): if not isinstance(wqx_metadata, WqxOutbound): wqx_metadata = WqxOutbound(wqx_metadata) if not isinstance(wqx_data, WqxOutbound): wqx_data = WqxOutbound(wqx_data) if wqx_data.failed or wqx_metadata.failed: self.feature = None else: s = Station() s.uid = wqx_metadata.location.id s.name = wqx_metadata.location.name s.set_property("station_type", wqx_metadata.location.type) s.set_property("location_description", wqx_metadata.location.description) s.set_property("huc", wqx_metadata.location.huc) s.set_property("county", wqx_metadata.location.county) s.set_property("state", wqx_metadata.location.state) s.set_property("country", wqx_metadata.location.country) s.set_property("organization_id", wqx_metadata.organization.id) s.set_property("organization_name", wqx_metadata.organization.name) s.set_property("vertical_units", wqx_metadata.location.vertical_measure_units) s.set_property("horizontal_crs", wqx_metadata.location.horizontal_crs_name) s.set_property("vertical_crs", wqx_metadata.location.vertical_crs_name) for a in wqx_data.activities: p = Point() p.time = a.start_time for r in a.results: p.add_member(Member(value=r.value, unit=r.units, name=r.name, description=r.short_name, standard=None, quality=r.quality, method_id=a.method_id, method_name=a.method_name)) s.add_element(p) # Now set the station's location vertical = 0 try: vertical = float(wqx_metadata.location.vertical_measure_value) except: pass # convert the vertical to meters if it is ft (which it always is) if wqx_metadata.location.vertical_measure_units == "ft": vertical /= 3.28084 s.set_property("vertical_units", "m") s.location = sPoint(float(wqx_metadata.location.longitude), float(wqx_metadata.location.latitude), vertical) self.feature = s
def _get_profile(self, sensor, t): sens_loc_tuple = (sensor['location']['point'].x, sensor['location']['point'].y, sensor['station']) profile_od = self._cache[sens_loc_tuple] if t not in profile_od: profile = Profile() profile.location = sPoint(*sens_loc_tuple[0:2]) profile.time = t # @TODO this is a hack until we can figure out how to assoc stations properly profile.station = sensor['station'] profile_od[t] = profile return profile return profile_od[t]
def _paths_to_cost(self): print "Analyzing paths..." count = 0 for path in self.paths: count += 1 print "\t%d of %d" % (count, len(self.paths)) distances = np.empty((self.height, self.width)) distances.fill(np.inf) min_x, min_y, max_x, max_y = path.bounds faraway = 5 min_x -= faraway; min_y -= faraway max_x += faraway; max_y += faraway print "\t\tPath bounding box area: %fm by %fm" % ( abs(max_x - min_x), abs(max_y - min_y) ) for x in np.arange(min_x, max_x, self.resolution): for y in np.arange(min_y, max_y, self.resolution): i, j = self._world_to_map(x, y) pt = sPoint(x, y) distances[i, j] = path.distance(pt) self.cost_grid += np.exp(-(distances ** 2))
def groupNearbyCrossRoads(crossRoads, radius): """ crossRoads as a geojson feature collection using UTM coords for metric distance and also setting the edgeCount for every CrossRoad """ shapelyPoints = [] graph = nx.Graph() for location, newProperties in crossRoads.items(): point = sPoint(wgsToUtm(*location)) point.id = location graph.add_node(location) shapelyPoints.append(point) index = STRtree(shapelyPoints) for point in shapelyPoints: searchArea = point.buffer(radius) crossRoadPoints = [p for p in index.query(searchArea) if not p == point] for otherPoint in crossRoadPoints: graph.add_edge(otherPoint.id, point.id) result = crossRoads.copy() components = [component for component in nx.connected_components(graph) if not len(component) == 1] for crossRoadLocations in components: nearbyCrossRoads = {loc: result.pop(loc) for loc in crossRoadLocations} center = tuple(sMultiPoint(list(crossRoadLocations)).centroid.coords[0]) newProperties = CrossRoadProperties() for oldProp in nearbyCrossRoads.values(): newProperties.union(oldProp) newProperties.computeEdgeCount() result[center] = newProperties return crossRoadsToFeatures(result)
def __init__(self, element): record = DataRecord(element) # Top level org structure stations_field = record.get_by_name("stations") stations = {} sensors = {} for station in stations_field.content.field: s = Station() s.name = station.name s.uid = station.content.get_by_name("stationID").content.value # Location vector = station.content.get_by_name("platformLocation").content srss = vector.referenceFrame.split("&") hsrs = None try: hsrs = Crs(srss[0]) except ValueError: pass vsrs = None try: vsrs = Crs(srss[-1].replace("2=http:", "http:")) except ValueError: pass s.set_property("horizontal_srs", hsrs) s.set_property("vertical_srs", vsrs) s.set_property("localFrame", vector.localFrame) lat = vector.get_by_name("latitude").content.value lon = vector.get_by_name("longitude").content.value z = vector.get_by_name("height").content.value loc = [lon, lat] if z: loc.append(z) s.location = sPoint(*loc) # Sensors for sensor in station.content.get_by_name("sensors").content.field: name = sensor.name uri = sensor.content.get_by_name("sensorID").content.value height = None location_quantity = sensor.content.get_by_name("height").content if location_quantity.referenceFrame == "#%s_frame" % s.name: # Uses the station as reference frame if location_quantity.value and z: height = z + location_quantity.value horizontal_srs = s.get_property("horizontal_srs") vertical_srs = s.get_property("vertical_srs") else: # Uses its own height if location_quantity.value: height = location_quantity.value horizontal_srs = None vertical_srs = None if hasattr(sensor, 'referenceFrame'): srss = sensor.referenceFrame.split("&") try: horizontal_srs = Crs(srss[0]) except ValueError: pass try: vertical_srs = Crs(srss[-1].replace("2=http:", "http:")) except ValueError: pass loc = [s.location.x, s.location.y] if height: loc.append(height) location = sPoint(*loc) sensors[name] = { 'station' : s.uid, 'name' : name, 'uri' : uri, 'horizontal_srs' : horizontal_srs, 'vertical_srs' : vertical_srs, 'location' : location, 'columns' : [], # Array of Members representing the columns 'values' : [] # Array of Points (the actual data) } stations[s.uid] = s # Start building the column structure data_array = record.get_by_name("observationData").content data_record = data_array.elementType.content columns = [] # Data outside of the <field name="sensors"> DataChoice element for f in data_record.field: columns.append(f) # Data inside of DataChoice sensor_data = data_record.get_by_name("sensor") for sendata in sensor_data.content.item: if sendata.content is not None: sensors[sendata.name]['columns'] = [] sensors[sendata.name]['values'] = [] for f in sendata.content.field: # Create a model Member for each column that will be copied and filled with data from each row sensors[sendata.name]['columns'].append(f) decimalSeparator = data_array.encoding.decimalSeparator tokenSeparator = data_array.encoding.tokenSeparator blockSeparator = data_array.encoding.blockSeparator collapseWhiteSpaces = data_array.encoding.collapseWhiteSpaces data_values = data_array.values self.raw_data = copy(data_values) for row in filter(lambda x: x != "", data_values.split(blockSeparator)): pt = None members = [] values = row.split(tokenSeparator) sensor_key = None i = 0 for x in columns: if isinstance(x.content, Time) and x.content.definition == "http://www.opengis.net/def/property/OGC/0/SamplingTime": pt = Point() pt.time = parser.parse(values[i]) elif isinstance(x.content, DataChoice): sensor_key = values[i] dc_cols = sensors[sensor_key]['columns'] for j,c in enumerate(dc_cols): if isinstance(c.content, AbstractSimpleComponent): m = Member( units=c.content.uom, name=c.name, standard=c.content.definition, value=float(values[i+1])) members.append(m) elif isinstance(c.content, Time) and c.content.definition == "http://www.opengis.net/def/property/OGC/0/SamplingTime": pt = Point() pt.time = parser.parse(v) # For each data column i += 1 elif isinstance(x.content, AbstractSimpleComponent): m = Member( units=x.content.uom, name=x.name, standard=x.content.definition, value=float(values[i])) members.append(m) else: print "WHAT AM I" i += 1 pt.members = members pt.location = stations[sensors[sensor_key]['station']].location sensors[sensor_key]['values'].append(pt) for k,v in stations.iteritems(): for sk, sv in sensors.iteritems(): # Match on station uid if sv['station'] == k: v.elements = self._merge_points(v.elements or [], sv['values']) if len(stations) > 1: self.feature = StationCollection(elements=stations) elif len(stations) == 1: self.feature = next(stations.itervalues()) else: print "No stations found!"
def _build_station_collection(self, parsed_metadata, parsed_data): stations = [] for station_code, station_metadata in parsed_metadata.iteritems(): s = Station() s.uid = station_code s.name = station_metadata['nwsli'] s.location = sPoint(station_metadata['longitude'], station_metadata['latitude'], 0) # hads always vertically zero s.set_property("location_description" , station_metadata['location_text']) s.set_property("state" , station_metadata['state']) s.set_property("country" , "USA") # @TODO s.set_property("vertical_units" , "ft") s.set_property("horizontal_crs" , "EPSG:4326") s.set_property("vertical_crs" , None) s.set_property("hsa" , station_metadata['hsa']) s.set_property("init_transmit" , station_metadata['init_transmit']) s.set_property("manufacturer" , station_metadata['manufacturer']) s.set_property("owner" , station_metadata['owner']) s.set_property("channel" , station_metadata['channel']) stations.append(s) # data # possibility no data for this station, or vars filtered all out if station_code not in parsed_data: continue # need to group into distinct time/z value pairs # create a keyfunc (creates string of <z>-<timestamp>) zandtime = lambda x: str(x[3]) + "-" + str(time.mktime(x[1].timetuple())) # annotate data with z values, sort, group by keyfunc (z/time) grouped_data = groupby(sorted(map(lambda x: (x[0], x[1], x[2], parsed_metadata[station_code]['variables'][x[0]]['base_elevation']), parsed_data[station_code]), key=zandtime), zandtime) for _, group in grouped_data: # group is an iterator, turn it into a list (it will have at least one item) groupvals = list(group) p = Point() p.time = groupvals[0][1] p.location = sPoint(station_metadata['longitude'], station_metadata['latitude'], groupvals[0][3]) for val in groupvals: std_var = self.get_variable_info(val[0]) if std_var is None: print "Unknown PE Code, ignoring:", val[0], "(station:", station_code, ")" continue p.add_member(Member(value=val[2], standard=std_var[0], unit=std_var[1], name=std_var[2], description=std_var[3])) s.add_element(p) return StationCollection(elements=stations)
def __init__(self, waterml_data): if isinstance(waterml_data, str) or isinstance(waterml_data, unicode): try: self._root = etree.fromstring(str(waterml_data)) except ValueError: # Strip out the XML header due to UTF8 encoding declaration self._root = etree.fromstring(waterml_data[56:]) else: self._root = waterml_data response = WaterML_1_1(self._root).response stations = [] station_lookup = [] for timeseries in response.time_series: station_code = sorted(timeseries.source_info.site_codes)[0] # Create station if we have not seen it if station_code not in station_lookup: s = Station() s.uid = station_code info = timeseries.source_info s.name = info.site_name s.set_property("station_type", info.site_types) s.set_property("huc", info.site_properties.get("hucCd")) s.set_property("county", info.site_properties.get("countyCd")) s.set_property("state", info.site_properties.get("stateCd")) # Now set the station's location vertical = info.elevation if vertical is None: vertical = 0 try: location = info.location.geo_coords[0] srs = info.location.srs[0] except: print "Could not find a location for %s... skipping station" % s.uid continue s.location = sPoint(float(location[0]), float(location[1]), vertical) s.set_property("horizontal_crs", srs) s.set_property("vertical_units", "m") s.set_property("vertical_crs", info.vertical_datum) s.set_property("location_description", info.location.notes) stations.append(s) station_lookup.append(s.uid) times = {} variable = timeseries.variable for variable_timeseries in timeseries.values: for r in variable_timeseries: dt = r.date_time if dt.tzinfo is None: dt = dt.replace(tzinfo=pytz.utc) dt = dt.astimezone(pytz.utc) if dt not in times.keys(): times[dt] = [] times[dt].append(Member(value=r.value, unit=variable.unit.code, name=variable.variable_name, description=variable.variable_description, standard=variable.variable_code)) station = stations[station_lookup.index(station_code)] for dts,members in times.iteritems(): p = Point() p.time = dts p.location = station.location p.members = members station.add_element(p) self.feature = StationCollection(elements=stations)
def draw_scatter( ds: DiagramSettings, canvas: svgwrite.drawing.Drawing, plot: ScatterPlot, xmapping: IntervalMapping, ) -> svgwrite.container.Group: """ given a xmapping, draw the scatter plot svg group Args: ds: the settings/constants to use for building the svg canvas: the svgwrite object used to create new svg elements plot: the plot to be drawn xmapping: dict used for conversion of coordinates in the xaxis to pixel positions """ from shapely.geometry import Point as sPoint # generate the y coordinate mapping plot_group = canvas.g(class_='scatter_plot') yratio = plot.height / (abs(plot.ymax - plot.ymin)) px_points = [] circles = [] for x_pos, y_pos in plot.points: try: x_px = Interval.convert_ratioed_pos(xmapping, x_pos, forward_to_reverse=False) y_px = Interval(plot.height - abs(min(y_pos, plot.ymax) - plot.ymin) * yratio) current_circle = sPoint(x_px.center, y_px.center).buffer( ds.scatter_marker_radius) if circles: ratio = circles[-1].intersection( current_circle).area / current_circle.area if ratio > plot.density: continue circles.append(current_circle) px_points.append(( x_px, y_px, plot.ymax_color if y_pos > plot.ymax else plot.colors.get( (x_pos, y_pos), '#000000'), )) except IndexError: pass logger.info( f'drew {len(circles)} of {len(plot.points)} points (density={plot.density})' ) for x_px, y_px, color in px_points: if x_px.length() > ds.scatter_marker_radius: plot_group.add( canvas.line( (x_px.start, y_px.center), (x_px.end, y_px.center), stroke='#000000', stroke_width=ds.scatter_error_bar_stroke_width, )) if y_px.length() > ds.scatter_marker_radius: plot_group.add( canvas.line( (x_px.center, y_px.start), (x_px.center, y_px.end), stroke='#000000', stroke_width=ds.scatter_error_bar_stroke_width, )) plot_group.add( canvas.circle(center=(x_px.center, y_px.center), fill=color, r=ds.scatter_marker_radius)) xmax = Interval.convert_ratioed_pos(xmapping, plot.xmax, forward_to_reverse=False).end for py in plot.hmarkers: py = plot.height - abs(py - plot.ymin) * yratio plot_group.add( canvas.line(start=(0, py), end=(xmax, py), stroke='blue')) # draw left y axis plot_group.add( canvas.line(start=(0, 0), end=(0, plot.height), stroke='#000000')) ytick_labels = [0] # draw start and end markers on the y axis for y in plot.yticks: ytick_labels.append(len(str(y))) py = plot.height - abs(y - plot.ymin) * yratio plot_group.add( canvas.line(start=(0 - ds.scatter_yaxis_tick_size, py), end=(0, py), stroke='#000000')) plot_group.add( canvas.text( str(y), insert=( 0 - ds.scatter_yaxis_tick_size - ds.padding, py + ds.scatter_ytick_font_size * ds.font_central_shift_ratio, ), fill=ds.label_color, style=ds.font_style.format( font_size=ds.scatter_ytick_font_size, text_anchor='end'), )) shift = max(ytick_labels) x = (0 - ds.padding * 2 - ds.scatter_axis_font_size - ds.scatter_yaxis_tick_size - ds.scatter_ytick_font_size * ds.font_width_height_ratio * shift) y = plot.height / 2 yaxis = canvas.text( plot.y_axis_label, insert=(x, y), fill=ds.label_color, style=ds.font_style.format(font_size=ds.scatter_axis_font_size, text_anchor='start'), class_='y_axis_label', ) plot_group.add(yaxis) cx = len(plot.y_axis_label ) * ds.font_width_height_ratio * ds.scatter_axis_font_size / 2 yaxis.rotate(270, (x + cx, y)) yaxis.translate(0, 0) y = plot.height setattr(plot_group, 'height', y) return plot_group
angle_sum = Dec(0) for v in vertices: angle_sum += v.ang theoretical_angle_sum = Dec(int((len(vertices)-2)*180)) # 9-kampio krastine D1 D1 = Dec('174.667') + C # Daugiakampio pasukimo kampas (K1) K1 = Dec('13.147') + B # Atstumas iki tikrosios uzliejimo zonos (A1) (0.001 tikslumu) A1 = Dec('67.536') + B circle_radius = float(D1)/2/sin(pi/7)-float(A1) heptagon_area = heptagon(float(D1)).area circle_area = sPoint(0,0).buffer(circle_radius).area # Points is vertice map by id Points = {} for v in vertices: Points[v.point] = v CONTINUOUS = (1,0) DASHDOTX2 = (10,3,2,3) DASHED = (100,20) keliai = { 'A-08': kelias( virsunes=[1,2,3], plotis=A08_plotis, kat=KAT1,
def __init__(self, element): record = DataRecord(element) stations_field = record.get_by_name("stations") stations = {} sensors = {} for station in stations_field.content.field: s = StationProfile() s.name = station.name s.uid = station.content.get_by_name("stationID").content.value # Location vector = station.content.get_by_name("platformLocation").content srss = vector.referenceFrame.split("&") hsrs = None try: hsrs = Crs(srss[0]) except ValueError: pass vsrs = None try: vsrs = Crs(srss[-1].replace("2=http:", "http:")) except ValueError: pass s.set_property("horizontal_srs", hsrs) s.set_property("vertical_srs", vsrs) s.set_property("localFrame", vector.localFrame) lat = vector.get_by_name("latitude").content.value lon = vector.get_by_name("longitude").content.value z = vector.get_by_name("height").content.value loc = [lon, lat] if z: loc.append(z) s.location = sPoint(*loc) # sensors for sensor in station.content.get_by_name("sensors").content.field: name = sensor.name uri = sensor.content.get_by_name("sensorID").content.value sensors[name] = {'station' : s.uid, 'name' : name, 'uri' : uri} # orientation ori_el = sensor.content.get_by_name("sensorOrientation") if ori_el: orientation = self._parse_sensor_orientation(ori_el) sensors[name]['sensor_orientation'] = orientation # location loc_el = sensor.content.get_by_name("sensorLocation") if loc_el: location = self._parse_location(loc_el, s.location) sensors[name]['location'] = location # profile bins profbins_el = sensor.content.get_by_name("profileBins") if profbins_el: profile_bins = self._parse_profile_bins(profbins_el) sensors[name]['profile_bins'] = profile_bins # OR profile heights profheights_el = sensor.content.get_by_name('profileHeights') if profheights_el: profile_heights = self._parse_profile_heights(profheights_el) sensors[name]['profile_heights'] = profile_heights s.sensors = sensors stations[s.uid] = s sensor_data = self._parse_sensor_data(record.get_by_name('observationData'), sensors) # sensor data is dict of station id -> profile collection for station_id, sensor_profile_data in sensor_data.iteritems(): stations[station_id].elements.extend(sensor_profile_data._elements) if len(stations) > 1: self.feature = StationCollection(elements=stations) elif len(stations) == 1: self.feature = next(stations.itervalues()) else: print "No stations found!" self.feature = None
def __init__(self, element): record = DataRecord(element) # Top level org structure stations_field = record.get_by_name("stations") stations = {} sensors = {} for station in stations_field.content.field: s = Station() s.name = station.name s.uid = station.content.get_by_name("stationID").content.value # Location vector = station.content.get_by_name("platformLocation").content srss = vector.referenceFrame.split("&") hsrs = None try: hsrs = Crs(srss[0]) except ValueError: pass vsrs = None try: vsrs = Crs(srss[-1].replace("2=http:", "http:")) except ValueError: pass s.set_property("horizontal_srs", hsrs) s.set_property("vertical_srs", vsrs) s.set_property("localFrame", vector.localFrame) lat = vector.get_by_name("latitude").content.value lon = vector.get_by_name("longitude").content.value z = vector.get_by_name("height").content.value loc = [lon, lat] if z: loc.append(z) s.location = sPoint(*loc) # Sensors for sensor in station.content.get_by_name("sensors").content.field: name = sensor.name uri = sensor.content.get_by_name("sensorID").content.value height = None location_quantity = sensor.content.get_by_name( "height").content if location_quantity.referenceFrame == "#%s_frame" % s.name: # Uses the station as reference frame if location_quantity.value and z: height = z + location_quantity.value horizontal_srs = s.get_property("horizontal_srs") vertical_srs = s.get_property("vertical_srs") else: # Uses its own height if location_quantity.value: height = location_quantity.value horizontal_srs = None vertical_srs = None if hasattr(sensor, 'referenceFrame'): srss = sensor.referenceFrame.split("&") try: horizontal_srs = Crs(srss[0]) except ValueError: pass try: vertical_srs = Crs(srss[-1].replace( "2=http:", "http:")) except ValueError: pass loc = [s.location.x, s.location.y] if height: loc.append(height) location = sPoint(*loc) sensors[name] = { 'station': s.uid, 'name': name, 'uri': uri, 'horizontal_srs': horizontal_srs, 'vertical_srs': vertical_srs, 'location': location, 'columns': [], # Array of Members representing the columns 'values': [] # Array of Points (the actual data) } stations[s.uid] = s # Start building the column structure data_array = record.get_by_name("observationData").content data_record = data_array.elementType.content columns = [] # Data outside of the <field name="sensors"> DataChoice element for f in data_record.field: columns.append(f) # Data inside of DataChoice sensor_data = data_record.get_by_name("sensor") for sendata in sensor_data.content.item: if sendata.content is not None: sensors[sendata.name]['columns'] = [] sensors[sendata.name]['values'] = [] for f in sendata.content.field: # Create a model Member for each column that will be copied and filled with data from each row sensors[sendata.name]['columns'].append(f) decimalSeparator = data_array.encoding.decimalSeparator tokenSeparator = data_array.encoding.tokenSeparator blockSeparator = data_array.encoding.blockSeparator collapseWhiteSpaces = data_array.encoding.collapseWhiteSpaces data_values = data_array.values self.raw_data = copy(data_values) for row in filter(lambda x: x != "", data_values.split(blockSeparator)): pt = None members = [] values = row.split(tokenSeparator) sensor_key = None i = 0 for x in columns: if isinstance( x.content, Time ) and x.content.definition == "http://www.opengis.net/def/property/OGC/0/SamplingTime": pt = Point() pt.time = parser.parse(values[i]) elif isinstance(x.content, DataChoice): sensor_key = values[i] dc_cols = sensors[sensor_key]['columns'] for j, c in enumerate(dc_cols): if isinstance(c.content, AbstractSimpleComponent): m = Member(units=c.content.uom, name=c.name, standard=c.content.definition, value=float(values[i + 1])) members.append(m) elif isinstance( c.content, Time ) and c.content.definition == "http://www.opengis.net/def/property/OGC/0/SamplingTime": pt = Point() pt.time = parser.parse(values[i]) # For each data column i += 1 elif isinstance(x.content, AbstractSimpleComponent): m = Member(units=x.content.uom, name=x.name, standard=x.content.definition, value=float(values[i])) members.append(m) else: print "WHAT AM I" i += 1 pt.members = members pt.location = stations[sensors[sensor_key]['station']].location sensors[sensor_key]['values'].append(pt) for k, v in stations.iteritems(): for sk, sv in sensors.iteritems(): # Match on station uid if sv['station'] == k: v.elements = self._merge_points(v.elements or [], sv['values']) if len(stations) > 1: self.feature = StationCollection(elements=stations) elif len(stations) == 1: self.feature = next(stations.itervalues()) else: print "No stations found!"
def __init__(self, waterml_data): if isinstance(waterml_data, str) or isinstance(waterml_data, unicode): try: self._root = etree.fromstring(str(waterml_data)) except ValueError: # Strip out the XML header due to UTF8 encoding declaration self._root = etree.fromstring(waterml_data[56:]) else: self._root = waterml_data response = WaterML_1_1(self._root).response stations = [] station_lookup = [] for timeseries in response.time_series: station_code = sorted(timeseries.source_info.site_codes)[0] # Create station if we have not seen it if station_code not in station_lookup: s = Station() s.uid = station_code info = timeseries.source_info s.name = info.site_name s.set_property("station_type", info.site_types) s.set_property("huc", info.site_properties.get("hucCd")) s.set_property("county", info.site_properties.get("countyCd")) s.set_property("state", info.site_properties.get("stateCd")) # Now set the station's location vertical = info.elevation if vertical is None: vertical = 0 try: location = info.location.geo_coords[0] srs = info.location.srs[0] except: print "Could not find a location for %s... skipping station" % s.uid continue s.location = sPoint(float(location[0]), float(location[1]), vertical) s.set_property("horizontal_crs", srs) s.set_property("vertical_units", "m") s.set_property("vertical_crs", info.vertical_datum) s.set_property("location_description", info.location.notes) stations.append(s) station_lookup.append(s.uid) times = {} variable = timeseries.variable for variable_timeseries in timeseries.values: for r in variable_timeseries: dt = r.date_time if dt.tzinfo is None: dt = dt.replace(tzinfo=pytz.utc) dt = dt.astimezone(pytz.utc) if dt not in times.keys(): times[dt] = [] times[dt].append( Member(value=r.value, unit=variable.unit.code, name=variable.variable_name, description=variable.variable_description, standard=variable.variable_code)) station = stations[station_lookup.index(station_code)] for dts, members in times.iteritems(): p = Point() p.time = dts p.location = station.location p.members = members station.add_element(p) self.feature = StationCollection(elements=stations)
prev_dirang = float(K1) * pi / 180 step = 5 / 7 * pi heptagon = [np.array(Points[6].xy)] for i in range(1, 7): dxy = np.array( [float(D1) * cos(prev_dirang), float(D1) * sin(prev_dirang)]) heptagon.append(heptagon[i - 1] + dxy) prev_dirang += pi - step ax.add_patch( PolygonPatch(asPolygon(heptagon), linewidth=2, fc='xkcd:white', ec='xkcd:magenta')) # septynkampio centras x0, y0 = Points[6].xy x = x0 + float(D1) / (2 * sin(pi / 7)) * sin(pi / 7 - float(K1) * pi / 180) y = y0 + float(D1) / (2 * sin(pi / 7)) * cos(pi / 7 - float(K1) * pi / 180) center = sPoint(x, y) # užlieta erdvė apskritimas radius = float(D1) / 2 / sin(pi / 7) - float(A1) angles = np.linspace(0, 2 * pi, num=360) circle_y = y + np.sin(angles) * radius circle_x = x + np.cos(angles) * radius ax.plot(circle_x, circle_y) if __name__ == '__main__': plt.show()
def __init__(self, **kwargs): super(SweTimeSeries, self).__init__(**kwargs) # Parse out GML point. Defaults to 0 depth if none specified self.geo_srs = Crs( testXMLAttribute(self._location.find(nsp("Point", self.GML_NS)), 'srsName')) geo = [ float(v) for v in testXMLValue( self._location.find(nsp("Point/pos", self.GML_NS))).split(" ") ] if self.geo_srs.axisorder == "yx": self.geo = sPoint(geo[1], geo[0]) else: self.geo = sPoint(geo[0], geo[1]) try: self.geo.z = geo[2] except: pass pc = PointCollection() for row in self.results.data: p = Point() time = None z = None lat = None lon = None for field in row: if field.axis == "time": t = dateutil.parser.parse(field.value) p.time = t.astimezone(dateutil.tz.tzutc()) elif field.axis == "Long": lon = field elif field.axis == "Lat": lat = field elif field.axis == "h": z = field else: m = Member(value=field.value, unit=field.units, units_definition=field.units_url, name=field.name, definition=field.definition, standard=field.definition) p.add_member(m) # Set the spatial point if lon.srs != lat.srs: raise ValueError( "Longitude and Latitude need to have the same SRS/CRS!") p.location = sPoint(float(lon.value), float(lat.value), float(z.value)) pc.add_element(p) self.data = pc
def _build_station_collection(self, parsed_metadata, parsed_data): stations = [] for station_code, station_metadata in parsed_metadata.items(): s = Station() s.uid = station_code s.name = station_metadata['nwsli'] s.location = sPoint(station_metadata['longitude'], station_metadata['latitude'], 0) # hads always vertically zero s.set_property("location_description", station_metadata['location_text']) s.set_property("state", station_metadata['state']) s.set_property("country", "USA") # @TODO s.set_property("vertical_units", "ft") s.set_property("horizontal_crs", "EPSG:4326") s.set_property("vertical_crs", None) s.set_property("hsa", station_metadata['hsa']) s.set_property("init_transmit", station_metadata['init_transmit']) s.set_property("manufacturer", station_metadata['manufacturer']) s.set_property("owner", station_metadata['owner']) s.set_property("channel", station_metadata['channel']) stations.append(s) # data # possibility no data for this station, or vars filtered all out if station_code not in parsed_data: continue # need to group into distinct time/z value pairs # create a keyfunc (creates string of <z>-<timestamp>) zandtime = lambda x: str(x[3]) + "-" + str( time.mktime(x[1].timetuple())) # annotate data with z values, sort, group by keyfunc (z/time) grouped_data = groupby( sorted([(x[0], x[1], x[2], parsed_metadata[station_code] ['variables'][x[0]]['base_elevation']) for x in parsed_data[station_code]], key=zandtime), zandtime) for _, group in grouped_data: # group is an iterator, turn it into a list (it will have at least one item) groupvals = list(group) p = Point() p.time = groupvals[0][1] p.location = sPoint(station_metadata['longitude'], station_metadata['latitude'], groupvals[0][3]) for val in groupvals: std_var = self.get_variable_info(val[0]) if std_var is None: print("Unknown PE Code, ignoring: {} (station: {}).". format(val[0], station_code)) continue p.add_member( Member(value=val[2], standard=std_var[0], unit=std_var[1], name=std_var[2], description=std_var[3])) s.add_element(p) return StationCollection(elements=stations)
def __init__(self, awc_list): for awc_data in awc_list: try: self._root = etree.fromstring(awc_data.encode()) except Exception as e: warnings.warn( "Could not read from {!r}, got {}".format(awc_data, e) ) continue """Code to get station iterator goes here.""" stations = [] station_lookup = [] times = [] for metar in self._root.iter("METAR"): uid = metar.find("station_id").text if uid not in station_lookup: s = Station() s.uid = uid vertical = metar.find("elevation_m").text if vertical is None: vertical = 0 s.location = sPoint( float(metar.find("latitude").text), float(metar.find("longitude").text), float(vertical), ) s.set_property("metar_type", metar.find("metar_type").text) s.set_property("horizontal_crs", "GCS") s.set_property("vertical_units", "m") s.set_property("vertical_crs", "AGL") stations.append(s) station_lookup.append(s.uid) times.append({}) else: s = stations[station_lookup.index(uid)] variables = [ "elevation_m", "raw_text", "temp_c", "dewpoint_c", "wind_dir_degrees", "wind_speed_kt", "visibility_statute_mi", "altim_in_hg", "wx_string", "sky_condition", "flight_category", ] for variable in variables: time_string = metar.find("observation_time").text dt = datetime.strptime(time_string, "%Y-%m-%dT%H:%M:%SZ") if dt.tzinfo is None: dt = dt.replace(tzinfo=pytz.utc) dt = dt.astimezone(pytz.utc) if dt not in list( times[station_lookup.index(s.uid)].keys() ): times[station_lookup.index(s.uid)][dt] = [] if metar.find(variable): if variable in {"raw_text", "flight_category", "wx_string"}: times[station_lookup.index(s.uid)][dt].append( Member( value=metar.find(variable).text, unit=None, name=variable, description=variable, standard=None, ) ) elif variable == "sky_condition": sky_condition = [] for cond in metar.findall(variable): cover = cond.attrib["sky_cover"] try: alt = cond.attrib["cloud_base_ft_agl"] except Exception: alt = None sky_condition.append( { "sky_cover": cover, "cloud_base_ft_agl": alt, } ) times[station_lookup.index(s.uid)][dt].append( Member( value=sky_condition, unit="ft_above_ground_level", name=variable, description=variable, standard=None, ) ) else: times[station_lookup.index(s.uid)][dt].append( Member( value=float(metar.find(variable).text), unit=variable.split("_")[-1], name=variable, description=variable, standard=None, ) ) for time_dict, station in zip(times, stations): for dts, members in time_dict.items(): p = Point() p.time = dts p.location = station.location p.members = members station.add_element(p) self.feature = StationCollection(elements=stations)
def __init__(self, awc_list): for awc_data in awc_list: if isinstance(awc_data, str) or isinstance(awc_data, unicode): try: self._root = etree.fromstring(str(awc_data)) except ValueError: # Strip out the XML header due to UTF8 encoding declaration self._root = etree.fromstring(awc_data[56:]) else: raise ValueError("Cannot parse response into ElementTree xml object") '''Code to get station iterator goes here ''' stations = [] station_lookup = [] times = [] for metar in self._root.iter('METAR'): uid = metar.find("station_id").text if uid not in station_lookup: s = Station() s.uid = uid vertical = metar.find("elevation_m").text if vertical is None: vertical = 0 s.location = sPoint(float(metar.find("latitude").text), float(metar.find("longitude").text), float(vertical)) s.set_property("metar_type", metar.find("metar_type").text) s.set_property("horizontal_crs", "GCS") s.set_property("vertical_units", "m") s.set_property("vertical_crs", "AGL") stations.append(s) station_lookup.append(s.uid) times.append({}) else: s = stations[station_lookup.index(uid)] variables = ["elevation_m", "raw_text", "temp_c", "dewpoint_c", "wind_dir_degrees", "wind_speed_kt", "visibility_statute_mi", "altim_in_hg", "wx_string", "sky_condition", "flight_category"] for variable in variables: time_string = metar.find("observation_time").text dt = datetime.strptime(time_string, "%Y-%m-%dT%H:%M:%SZ") if dt.tzinfo is None: dt = dt.replace(tzinfo=pytz.utc) dt = dt.astimezone(pytz.utc) if dt not in times[station_lookup.index(s.uid)].keys(): times[station_lookup.index(s.uid)][dt] = [] if metar.find(variable) != None: if variable in set(["raw_text", "flight_category", "wx_string"]): times[station_lookup.index(s.uid)][dt].append(Member(value=metar.find(variable).text, unit=None, name=variable, description=variable, standard=None)) elif variable == "sky_condition": sky_condition = [] for cond in metar.findall(variable): cover = cond.attrib["sky_cover"] try: alt = cond.attrib["cloud_base_ft_agl"] except: alt = None sky_condition.append({"sky_cover" : cover, "cloud_base_ft_agl" : alt}) times[station_lookup.index(s.uid)][dt].append(Member(value=sky_condition, unit="ft_above_ground_level", name=variable, description=variable, standard=None)) else: times[station_lookup.index(s.uid)][dt].append(Member(value=float(metar.find(variable).text), unit=variable.split("_")[-1], name=variable, description=variable, standard=None)) for time_dict, station in zip(times, stations): for dts, members in time_dict.iteritems(): p = Point() p.time = dts p.location = station.location p.members = members station.add_element(p) self.feature = StationCollection(elements=stations)
def __init__(self, awc_list): for awc_data in awc_list: self._root = etree.fromstring(awc_data.encode()) '''Code to get station iterator goes here.''' stations = [] station_lookup = [] times = [] for metar in self._root.iter('METAR'): uid = metar.find("station_id").text if uid not in station_lookup: s = Station() s.uid = uid vertical = metar.find("elevation_m").text if vertical is None: vertical = 0 s.location = sPoint(float(metar.find("latitude").text), float(metar.find("longitude").text), float(vertical)) s.set_property("metar_type", metar.find("metar_type").text) s.set_property("horizontal_crs", "GCS") s.set_property("vertical_units", "m") s.set_property("vertical_crs", "AGL") stations.append(s) station_lookup.append(s.uid) times.append({}) else: s = stations[station_lookup.index(uid)] variables = [ "elevation_m", "raw_text", "temp_c", "dewpoint_c", "wind_dir_degrees", "wind_speed_kt", "visibility_statute_mi", "altim_in_hg", "wx_string", "sky_condition", "flight_category" ] for variable in variables: time_string = metar.find("observation_time").text dt = datetime.strptime(time_string, "%Y-%m-%dT%H:%M:%SZ") if dt.tzinfo is None: dt = dt.replace(tzinfo=pytz.utc) dt = dt.astimezone(pytz.utc) if dt not in list(times[station_lookup.index( s.uid)].keys()): times[station_lookup.index(s.uid)][dt] = [] if metar.find(variable) != None: if variable in set( ["raw_text", "flight_category", "wx_string"]): times[station_lookup.index(s.uid)][dt].append( Member(value=metar.find(variable).text, unit=None, name=variable, description=variable, standard=None)) elif variable == "sky_condition": sky_condition = [] for cond in metar.findall(variable): cover = cond.attrib["sky_cover"] try: alt = cond.attrib["cloud_base_ft_agl"] except: alt = None sky_condition.append({ "sky_cover": cover, "cloud_base_ft_agl": alt }) times[station_lookup.index(s.uid)][dt].append( Member(value=sky_condition, unit="ft_above_ground_level", name=variable, description=variable, standard=None)) else: times[station_lookup.index(s.uid)][dt].append( Member(value=float(metar.find(variable).text), unit=variable.split("_")[-1], name=variable, description=variable, standard=None)) for time_dict, station in zip(times, stations): for dts, members in time_dict.items(): p = Point() p.time = dts p.location = station.location p.members = members station.add_element(p) self.feature = StationCollection(elements=stations)
def test_profile(self): dt1 = datetime(2012, 1, 1, 12, 0) p1 = Point() p1.time = dt1 p1.location = sPoint(-120, 50, 0) member1 = Member(value=34.7, unit='°C', name='Water Temperatire', description='water temperature', standard='sea_water_temperature') member2 = Member(value=80, unit='PSU', name='Salinity', description='salinity', standard='salinity') p1.add_member(member1) p1.add_member(member2) dt2 = datetime(2012, 1, 1, 12, 10) p2 = Point() p2.time = dt2 p2.location = sPoint(-120, 50, 10) member3 = Member(value=34.1, unit='°C', name='Water Temperatire', description='water temperature', standard='sea_water_temperature') member4 = Member(value=70, unit='PSU', name='Salinity', description='salinity', standard='salinity') p2.add_member(member3) p2.add_member(member4) dt3 = datetime(2012, 1, 1, 12, 20) p3 = Point() p3.time = dt3 p3.location = sPoint(-120, 50, 20) member5 = Member(value=32.6, unit='°C', name='Water Temperatire', description='water temperature', standard='sea_water_temperature') member6 = Member(value=60, unit='PSU', name='Salinity', description='salinity', standard='salinity') p3.add_member(member5) p3.add_member(member6) pc = Profile(elements=[p1, p2, p3]) pc.location = sPoint(-120, 50) pc.time = dt1 pc.calculate_bounds() assert pc.size == 3 assert pc.time == dt1 assert len(pc.time_range) == 3 assert pc.time_range[0] == dt1 assert pc.time_range[-1] == dt3 assert len(pc.depth_range) == 3 assert pc.depth_range[0] == p1.location.z assert pc.depth_range[-1] == p3.location.z assert pc.upper_right().equals(pc.location) assert pc.lower_left().equals(pc.location)
def test_section(self): day = 1 hour = 0 sc = Section() dt = None # 10 profiles for x in xrange(0, 10): lat = random.randint(40, 44) lon = random.randint(-74, -70) loc = sPoint(lon, lat, 0) minute = 0 dt = datetime(2012, 4, day, hour, minute) hour += 1 prof = Profile() prof.location = loc prof.time = dt # Each with 20 depths for y in xrange(0, 20): p = Point() p.time = dt p.location = sPoint(loc.x, loc.y, y) m1 = Member( value=random.uniform(30, 40), unit="°C", name="Water Temperatire", description="water temperature", standard="sea_water_temperature", ) m2 = Member( value=random.uniform(80, 100), unit="PSU", name="Salinity", description="salinity", standard="salinity", ) p.add_member(m1) p.add_member(m2) prof.add_element(p) # Next depth is 2 minutes from now dt = dt + timedelta(minutes=2) sc.add_element(prof) sc.calculate_bounds() assert len(sc.get_path()) == 10 assert sc.size == 10 assert sc.point_size == 200 assert sc.type == "Section" assert len(sc.time_range) == 200 assert sc.time_range[0] == datetime(2012, 4, 1, 0, 0) assert sc.time_range[-1] == dt - timedelta(minutes=2) assert len(sc.depth_range) == 200 assert sc.depth_range[0] == 0 assert sc.depth_range[-1] == 19
def __init__(self, wqx_metadata, wqx_data): if not isinstance(wqx_metadata, WqxOutbound): wqx_metadata = WqxOutbound(wqx_metadata) if not isinstance(wqx_data, WqxOutbound): wqx_data = WqxOutbound(wqx_data) if wqx_data.failed or wqx_metadata.failed: self.feature = None else: s = Station() s.uid = wqx_metadata.location.id s.name = wqx_metadata.location.name s.set_property("station_type", wqx_metadata.location.type) s.set_property("location_description", wqx_metadata.location.description) s.set_property("huc", wqx_metadata.location.huc) s.set_property("county", wqx_metadata.location.county) s.set_property("state", wqx_metadata.location.state) s.set_property("country", wqx_metadata.location.country) s.set_property("organization_id", wqx_metadata.organization.id) s.set_property("organization_name", wqx_metadata.organization.name) s.set_property("vertical_units", wqx_metadata.location.vertical_measure_units) s.set_property("horizontal_crs", wqx_metadata.location.horizontal_crs_name) s.set_property("vertical_crs", wqx_metadata.location.vertical_crs_name) for a in wqx_data.activities: p = Point() p.time = a.start_time for r in a.results: p.add_member( Member(value=r.value, unit=r.units, name=r.name, description=r.short_name, standard=None, quality=r.quality, method_id=a.method_id, method_name=a.method_name)) s.add_element(p) # Now set the station's location vertical = 0 try: vertical = float(wqx_metadata.location.vertical_measure_value) except: pass # convert the vertical to meters if it is ft (which it always is) if wqx_metadata.location.vertical_measure_units == "ft": vertical /= 3.28084 s.set_property("vertical_units", "m") s.location = sPoint(float(wqx_metadata.location.longitude), float(wqx_metadata.location.latitude), vertical) self.feature = s