def __init__(self, provider, location): """Constructor. @param provider: provider from dataengine, for sending data @type provider: Provider @param location: location from dataengine @type: location: Location """ self._provider = provider self._location = location self._plugin_name = "openstreetmap" self._has_list = False self._places = None self._net_if = NetworkInterface(self) self._osm_hand = OSMHandler(self.AMENITY_SUSTENANCE, self._location, self) self._reader = QXmlSimpleReader() self._reader.setContentHandler(self._osm_hand) self._reader.setErrorHandler(self._osm_hand) self._amenity = "" for i in self.AMENITY_SUSTENANCE: self._amenity += "%s|" % i self._amenity = self._amenity.rstrip("|")
def importSAX(self, fname): error = None fh = None try: handler = SaxMovieHandler(self) parser = QXmlSimpleReader() parser.setContentHandler(handler) parser.setErrorHandler(handler) fh = QFile(fname) input = QXmlInputSource(fh) self.clear(False) if not parser.parse(input): raise ValueError(handler.error) except (IOError, OSError, ValueError) as e: error = "Failed to import: {0}".format(e) finally: if fh is not None: fh.close() if error is not None: return False, error self.__fname = QString() self.__dirty = True return True, "Imported {0} movie records from {1}".format( len(self.__movies), QFileInfo(fname).fileName())
def adjust(self): """ Export data to GNU Gama xml, adjust the network and read result :returns: result list of adjusment and blunder from GNU Gama """ # gama-local OK? if self.gama_path is None: logging.error("GNU gama path is None") return (None, None) # fix = 0 free network fix = sum([1 for p, s in self.points if s == 'FIX']) adj = sum([1 for p, s in self.points if s == 'ADJ']) if adj == 0 or len(self.observations) < 2: # no unknowns or observations logging.error("GNU gama no unknowns or not enough observations") return (None, None) doc = QDomDocument() doc.appendChild(doc.createComment('Gama XML created by Ulyxes')) gama_local = doc.createElement('gama-local') gama_local.setAttribute('version', '2.0') doc.appendChild(gama_local) network = doc.createElement('network') network.setAttribute('axes-xy', 'ne') network.setAttribute('angles', 'left-handed') gama_local.appendChild(network) description = doc.createElement('description') if self.dimension == 1: description.appendChild(doc.createTextNode('GNU Gama 1D network')) elif self.dimension == 2: description.appendChild(doc.createTextNode('GNU Gama 2D network')) elif self.dimension == 3: description.appendChild(doc.createTextNode('GNU Gama 3D network')) network.appendChild(description) parameters = doc.createElement('parameters') parameters.setAttribute('sigma-apr', '1') parameters.setAttribute('conf-pr', str(self.probability)) parameters.setAttribute('tol-abs', '1000') parameters.setAttribute('sigma-act', 'aposteriori') # parameters.setAttribute('sigma-act', 'apriori') parameters.setAttribute('update-constrained-coordinates', 'yes') network.appendChild(parameters) points_observations = doc.createElement('points-observations') points_observations.setAttribute( 'distance-stdev', str(self.stdev_dist) + ' ' + str(self.stdev_dist1)) points_observations.setAttribute( 'direction-stdev', str(self.stdev_angle / 3600.0 * 10000.0)) points_observations.setAttribute( 'angle-stdev', str(math.sqrt(2) * self.stdev_angle / 3600.0 * 10000)) points_observations.setAttribute( 'zenith-angle-stdev', str(self.stdev_angle / 3600.0 * 10000.0)) network.appendChild(points_observations) for p, s in self.points: if self.dimension == 1: tmp = doc.createElement('point') tmp.setAttribute('id', p['id']) if p['elev'] is not None: tmp.setAttribute('z', str(p['elev'])) if s == 'FIX': tmp.setAttribute('fix', 'z') else: if fix == 0: tmp.setAttribute('adj', 'Z') else: tmp.setAttribute('adj', 'z') points_observations.appendChild(tmp) elif self.dimension == 2: tmp = doc.createElement('point') tmp.setAttribute('id', p['id']) if p['east'] is not None and p['north'] is not None: tmp.setAttribute('y', str(p['east'])) tmp.setAttribute('x', str(p['north'])) if s == 'FIX': tmp.setAttribute('fix', 'xy') else: if fix == 0: # free network tmp.setAttribute('adj', 'XY') else: tmp.setAttribute('adj', 'xy') points_observations.appendChild(tmp) elif self.dimension == 3: tmp = doc.createElement('point') tmp.setAttribute('id', p['id']) if p['east'] is not None and p['north'] is not None: tmp.setAttribute('y', str(p['east'])) tmp.setAttribute('x', str(p['north'])) if p['elev'] is not None: tmp.setAttribute('z', str(p['elev'])) if s == 'FIX': tmp.setAttribute('fix', 'xyz') else: if fix == 0: tmp.setAttribute('adj', 'XYZ') else: tmp.setAttribute('adj', 'xyz') points_observations.appendChild(tmp) for o in self.observations: if 'station' in o: # station record sta = doc.createElement('obs') sta.setAttribute('from', o['station']) # instrument height ih = 0 if 'ih' in o: ih = o['ih'] points_observations.appendChild(sta) else: # observation th = 0 if 'th' in o: th = o['th'] if self.dimension == 2: # horizontal network if 'hz' in o: tmp = doc.createElement('direction') tmp.setAttribute('to', o['id']) tmp.setAttribute('val', str(o['hz'].GetAngle('GON'))) sta.appendChild(tmp) if 'distance' in o and 'v' in o: # horizontal distance hd = math.sin(o['v'].GetAngle()) * o['distance'] tmp = doc.createElement('distance') tmp.setAttribute('to', o['id']) tmp.setAttribute('val', str(hd)) sta.appendChild(tmp) elif self.dimension == 1: # elevations only pass elif self.dimension == 3: # 3d if 'hz' in o: tmp = doc.createElement('direction') tmp.setAttribute('to', o['id']) tmp.setAttribute('val', str(o['hz'].GetAngle('GON'))) sta.appendChild(tmp) if 'distance' in o: tmp = doc.createElement('s-distance') tmp.setAttribute('to', o['id']) tmp.setAttribute('val', str(o['distance'])) tmp.setAttribute('from_dh', str(ih)) tmp.setAttribute('to_dh', str(th)) sta.appendChild(tmp) if 'v' in o: tmp = doc.createElement('z-angle') tmp.setAttribute('to', o['id']) tmp.setAttribute('val', str(o['v'].GetAngle('GON'))) tmp.setAttribute('from_dh', str(ih)) tmp.setAttribute('to_dh', str(th)) sta.appendChild(tmp) else: # unknown dimension logging.error("GNU gama unknown dimension") return (None, None) # generate temp file name f = tempfile.NamedTemporaryFile() tmp_name = f.name f.close() f = open(tmp_name + '.xml', 'w') f.write(doc.toByteArray()) f.close() # run gama-local status = os.system(self.gama_path + ' ' + tmp_name + '.xml --text ' + tmp_name + '.txt --xml ' + tmp_name + 'out.xml') if status != 0: logging.error("GNU gama failed") return (None, None) xmlParser = QXmlSimpleReader() xmlFile = QFile(tmp_name + 'out.xml') xmlInputSource = QXmlInputSource(xmlFile) doc.setContent(xmlInputSource, xmlParser) # get adjusted coordinates adj_nodes = doc.elementsByTagName('adjusted') if adj_nodes.count() < 1: logging.error("GNU gama no adjusted coordinates") return (None, None) res = [] adj_node = adj_nodes.at(0) for i in range(len(adj_node.childNodes())): pp = adj_node.childNodes().at(i) if pp.nodeName() == 'point': p = {} for ii in range(len(pp.childNodes())): ppp = pp.childNodes().at(ii) if ppp.nodeName() == 'id': p['id'] = str(ppp.firstChild().nodeValue()) elif ppp.nodeName() == 'Y' or ppp.nodeName() == 'y': p['east'] = float(ppp.firstChild().nodeValue()) elif ppp.nodeName() == 'X' or ppp.nodeName() == 'x': p['north'] = float(ppp.firstChild().nodeValue()) elif ppp.nodeName() == 'Z' or ppp.nodeName() == 'z': p['elev'] = float(ppp.firstChild().nodeValue()) # TODO standard deviation of coords to p res.append(p) adj_nodes = doc.elementsByTagName('observations') if adj_nodes.count() < 1: logging.error("GNU gama no adjusted observations") return (None, None) blunder = {'std-residual': 0} adj_node = adj_nodes.at(0) for i in range(len(adj_node.childNodes())): pp = adj_node.childNodes().at(i) if pp.nodeName() in [ 'direction', 'slope-distance', 'zenith-angle' ]: o = {'std-residual': 0} for ii in range(len(pp.childNodes())): ppp = pp.childNodes().at(ii) if ppp.nodeName() == 'from': o['from'] = str(ppp.firstChild().nodeValue()) elif ppp.nodeName() == 'to': o['to'] = str(ppp.firstChild().nodeValue()) elif ppp.nodeName() == 'f': o['f'] = float(ppp.firstChild().nodeValue()) elif ppp.nodeName() == 'std-residual': o['std-residual'] = float(ppp.firstChild().nodeValue()) if o['std-residual'] > self.krit and \ o['std-residual'] > blunder['std-residual'] and \ o['f'] > 10: # extra observations ratio blunder = dict(o) xmlFile.close() # remove input xml and output xml os.remove(tmp_name + '.xml') os.remove(tmp_name + '.txt') os.remove(tmp_name + 'out.xml') return (res, blunder)
def adjust(self): """ Export data to GNU Gama xml, adjust the network and read result :returns: result list of adjusment from GNU Gama """ # fix = 0 free network fix = 0 adj = 0 for p, s in self.points: if s == 'FIX': fix += 1 else: adj += 1 if adj == 0 or len(self.observations) == 0: # no unknowns or observations return None doc = QDomDocument() doc.appendChild( doc.createComment( 'Gama XML created by SurveyingCalculation plugin for QGIS')) gama_local = doc.createElement('gama-local') gama_local.setAttribute('version', '2.0') doc.appendChild(gama_local) network = doc.createElement('network') network.setAttribute('axes-xy', 'ne') network.setAttribute('angles', 'left-handed') gama_local.appendChild(network) description = doc.createElement('description') if self.dimension == 1: description.appendChild(doc.createTextNode('GNU Gama 1D network')) elif self.dimension == 2: description.appendChild(doc.createTextNode('GNU Gama 2D network')) elif self.dimension == 3: description.appendChild(doc.createTextNode('GNU Gama 3D network')) network.appendChild(description) parameters = doc.createElement('parameters') parameters.setAttribute('sigma-apr', '1') parameters.setAttribute('conf-pr', str(self.probability)) parameters.setAttribute('tol-abs', '1000') parameters.setAttribute('sigma-act', 'aposteriori') parameters.setAttribute('update-constrained-coordinates', 'yes') network.appendChild(parameters) points_observations = doc.createElement('points-observations') points_observations.setAttribute( 'distance-stdev', str(self.stdev_dist) + ' ' + str(self.stdev_dist1)) points_observations.setAttribute('direction-stdev', str(self.stdev_angle)) points_observations.setAttribute('angle-stdev', str(math.sqrt(self.stdev_angle * 2))) points_observations.setAttribute('zenith-angle-stdev', str(self.stdev_angle)) network.appendChild(points_observations) for p, s in self.points: if self.dimension == 1: tmp = doc.createElement('point') tmp.setAttribute('id', p.id) if p.z is not None: tmp.setAttribute('z', str(p.z)) if s == 'FIX': tmp.setAttribute('fix', 'z') else: if fix == 0: tmp.setAttribute('adj', 'Z') else: tmp.setAttribute('adj', 'z') points_observations.appendChild(tmp) elif self.dimension == 2: tmp = doc.createElement('point') tmp.setAttribute('id', p.id) if p.e is not None and p.n is not None: tmp.setAttribute('y', str(p.e)) tmp.setAttribute('x', str(p.n)) if s == 'FIX': tmp.setAttribute('fix', 'xy') else: if fix == 0: # free network tmp.setAttribute('adj', 'XY') else: tmp.setAttribute('adj', 'xy') points_observations.appendChild(tmp) elif self.dimension == 3: tmp = doc.createElement('point') tmp.setAttribute('id', p.id) if p.e is not None and p.n is not None: tmp.setAttribute('y', str(p.e)) tmp.setAttribute('x', str(p.n)) if p.z is not None: tmp.setAttribute('z', str(p.z)) if s == 'FIX': tmp.setAttribute('fix', 'xyz') else: if fix == 0: tmp.setAttribute('adj', 'XYZ') else: tmp.setAttribute('adj', 'xyz') points_observations.appendChild(tmp) if self.dimension == 1: hd = doc.createElement('height-differences') points_observations.appendChild(hd) for o in self.observations: if o.station == 'station': # station record st_id = o.point_id if o.th is None: ih = 0 else: ih = o.th if self.dimension in [2, 3]: sta = doc.createElement('obs') sta.setAttribute('from', o.point_id) points_observations.appendChild(sta) else: # observation if self.dimension == 2: # horizontal network if o.hz is not None: tmp = doc.createElement('direction') tmp.setAttribute('to', o.point_id) tmp.setAttribute('val', str(o.hz.get_angle('GON'))) sta.appendChild(tmp) if o.d is not None: # horizontal distance hd = o.horiz_dist() if hd is not None: tmp = doc.createElement('distance') tmp.setAttribute('to', o.point_id) tmp.setAttribute('val', str(hd)) sta.appendChild(tmp) elif self.dimension == 1: # elevations only 1d if o.th is None: th = 0 else: th = o.th if o.d is not None and o.v is not None: tmp = doc.createElement('dh') tmp.setAttribute('from', st_id) tmp.setAttribute('to', o.point_id) # TODO hibaterjedes tmp.setAttribute('stdev', '1') sz = math.sin(o.v.get_angle()) w = self.stdev_dist + self.stdev_dist1 * o.d.d / 1000 ro_cc = 200 * 100 * 100 / math.pi if o.d.mode == 'SD': cz = math.cos(o.v.get_angle()) tmp.setAttribute('val', str(o.d.d * cz + ih - th)) tmp.setAttribute( 'stdev', str( math.sqrt(cz**2 * w**2 + (o.d.d * 1000)**2 * sz**2 * (self.stdev_angle / RO_CC)**2))) else: tz = math.tan(o.v.get_angle()) tmp.setAttribute( 'val', str(o.d.d / math.tan(o.v.get_angle()) + ih - th)) tmp.setAttribute( 'stdev', str( math.sqrt((1 / tz)**2 * w**2 + (o.d.d * 1000)**2 * (o.d.d * 1000)**2 * (1 / sz**2)**2 * (self.stdev_angle / RO_CC)**2))) hd.appendChild(tmp) elif self.dimension == 3: # 3d if o.th is None: th = 0 else: th = o.th if o.hz is not None: tmp = doc.createElement('direction') tmp.setAttribute('to', o.point_id) tmp.setAttribute('val', str(o.hz.get_angle('GON'))) sta.appendChild(tmp) if o.d is not None: if o.d.mode == 'SD': tmp = doc.createElement('s-distance') tmp.setAttribute('val', str(o.d.d)) tmp.setAttribute('from_dh', str(ih)) tmp.setAttribute('to_dh', str(th)) else: tmp = doc.createElement('distance') tmp.setAttribute('val', str(o.d.d)) tmp.setAttribute('to', o.point_id) sta.appendChild(tmp) if o.v is not None: tmp = doc.createElement('z-angle') tmp.setAttribute('to', o.point_id) tmp.setAttribute('val', str(o.v.get_angle('GON'))) tmp.setAttribute('from_dh', str(ih)) tmp.setAttribute('to_dh', str(th)) sta.appendChild(tmp) else: # unknown dimension return None # generate temp file name tmpf = QTemporaryFile(QDir.temp().absoluteFilePath('w')) tmpf.open(QIODevice.WriteOnly) tmpf.close() tmp_name = tmpf.fileName() f = QFile(tmp_name + '.xml') if f.open(QIODevice.WriteOnly): f.write(doc.toByteArray()) f.close() # run gama-local if self.gama_prog is None: return None status = QProcess.execute(self.gama_prog, [ tmp_name + '.xml', '--text', tmp_name + '.txt', '--xml', tmp_name + 'out.xml' ]) if status != 0: return None xmlParser = QXmlSimpleReader() xmlFile = QFile(tmp_name + 'out.xml') xmlInputSource = QXmlInputSource(xmlFile) doc.setContent(xmlInputSource, xmlParser) f_txt = QFile(tmp_name + '.txt') f_txt.open(QIODevice.ReadOnly) res = f_txt.readAll().data() f_txt.close() # store coordinates adj_nodes = doc.elementsByTagName('adjusted') if adj_nodes.count() < 1: return res adj_node = adj_nodes.at(0) for i in range(len(adj_node.childNodes())): pp = adj_node.childNodes().at(i) if pp.nodeName() == 'point': for ii in range(len(pp.childNodes())): ppp = pp.childNodes().at(ii) if ppp.nodeName() == 'id': p = Point(ppp.firstChild().nodeValue()) elif ppp.nodeName() == 'Y' or ppp.nodeName() == 'y': p.e = float(ppp.firstChild().nodeValue()) elif ppp.nodeName() == 'X' or ppp.nodeName() == 'x': p.n = float(ppp.firstChild().nodeValue()) elif ppp.nodeName() == 'Z' or ppp.nodeName() == 'z': p.z = float(ppp.firstChild().nodeValue()) ScPoint(p).store_coord(self.dimension) # remove input xml and output xml tmpf.remove() f_txt.remove() f.remove() xmlFile.remove() return res
class OSM(): """Main class for open street map.""" # Amenity set for food and drink AMENITY_SUSTENANCE = [ "restaurant", "food_court", "fast_food", "drinking_water", "bbq", "pub", "bar", "cafe", "biergarten", ] def __init__(self, provider, location): """Constructor. @param provider: provider from dataengine, for sending data @type provider: Provider @param location: location from dataengine @type: location: Location """ self._provider = provider self._location = location self._plugin_name = "openstreetmap" self._has_list = False self._places = None self._net_if = NetworkInterface(self) self._osm_hand = OSMHandler(self.AMENITY_SUSTENANCE, self._location, self) self._reader = QXmlSimpleReader() self._reader.setContentHandler(self._osm_hand) self._reader.setErrorHandler(self._osm_hand) self._amenity = "" for i in self.AMENITY_SUSTENANCE: self._amenity += "%s|" % i self._amenity = self._amenity.rstrip("|") def start(self): """Starts the downloading process.""" #url = "http://xapi.openstreetmap.org" \ #url = "http://osm.bearstech.com" \ url = "http://osmxapi.hypercube.telascience.org" \ "/api/0.6/node[amenity=%s][bbox=%s]" % \ (self._amenity, self._location.getBox()) self._has_list = False self._places = None self._osm_hand.clear_places() try: self._net_if.download(url) except Exception as inst: self.send_error(inst) def receive_data(self, data): """Method called from network interface with new data. @param data: downloaded data @type data: str """ if not self._has_list: self._process_main_list(data) return source = QXmlInputSource() source.setData(data) try: self._reader.parse(source) except Exception: pass def _process_main_list(self, data): """Process main list from xapi.openstreetmap.org @param data: downloaded data with main list @type data: str """ try: source = QXmlInputSource() source.setData(data) self._reader.parse(source) self._has_list = True self._places = self._osm_hand.get_places() except Exception: self.send_error("Invalid Response.") return if len(self._places) == 0: self.send_error("No place found.") return for place in self._places: try: url = "http://nominatim.openstreetmap.org" \ "/reverse?format=xml&osm_type=N&osm_id=%s" % place.get_osm_id() self._net_if.download(url) print "%s, %s" % (place.get_name(), place.get_amenity()) except Exception: continue def send_place(self, osm_id): """Method for sending data to dataengine. Called from OSMHandler when place is ready for sending. Before sending counts distance from current location to place. @param osm_id: id of place @type osm_id: str """ # Send place types in categories self._provider.setProperty(self._plugin_name, "food_and_drink", self._amenity) place = self._places.search(osm_id) if place.is_complete(): # Count distance try: place.set_distance(distance(self._location.getLatitude(), self._location.getLongitude(), place.get_lat(), place.get_lon())) # If place is far away if (float(place.get_distance()) > \ (self._location.getRange() * 1000)): return # Send distance self._provider.setProperty(self._plugin_name, place.get_distance_id(), place.get_distance()) except Exception: pass # Send rest of data # Main name self._provider.setProperty(self._plugin_name, place.get_osm_id_key(), place.get_display_name()) # Type self._provider.setProperty(self._plugin_name, place.get_type_id(), place.get_type()) # Latitude and Longitude self._provider.setProperty(self._plugin_name, place.get_latlng_id(), place.get_latlng()) # Url self._provider.setProperty(self._plugin_name, place.get_url_id(), place.get_url()) # Website if place.get_website(): self._provider.setProperty(self._plugin_name, place.get_website_id(), place.get_website()) # Cuisine if place.get_cuisine(): self._provider.setProperty(self._plugin_name, place.get_cuisine_id(), place.get_cuisine()) # Opening hours if place.get_opening_hours(): self._provider.setProperty(self._plugin_name, place.get_opening_hours_id(), place.get_opening_hours()) self._provider.done(self._plugin_name) def send_error(self, err): """Send information about error to provider. @param err: error @type err: str """ print err self._provider.setProperty(self._plugin_name, "error", str(err))