def CreatePhotoOverlay(kml_doc, file_name, the_file, file_iterator, resizeWidth): """Creates a PhotoOverlay element in the kml_doc element. Args: kml_doc: An XML document object. file_name: The name of the file. the_file: The file object. file_iterator: The file iterator, used to create the id. Returns: An XML element representing the PhotoOverlay. """ photo_id = 'photo%s' % file_iterator folderId = '-'.join(splitall(file_name)[:-1]) path, filename = os.path.split(file_name) fh = GetFile(file_name) tags = exifread.process_file(fh, details=False) fh.close() if not os.path.exists('geoPhotos'): os.mkdir('geoPhotos') print("Opening: " + file_name) im = Image.open(file_name) try: exif = im.info['exif'] except: exif = False print(' No Exif Information') #Get image width and height and scale based on user width width, height = im.size newHeight = height / (width / resizeWidth) newsize = (int(resizeWidth), int(newHeight)) im1 = im.resize(newsize) parts = filename.split('.') longname = path.replace("/", '-') today = datetime.now() smallFileName = today.strftime( "%Y%m%d") + '-' + longname + '-' + parts[0] + '_small.jpg' smallFileName = smallFileName.replace("--", "-") if exif: im1 = im1.save('geoPhotos/' + smallFileName, 'JPEG', exif=exif) else: im1 = im1.save('geoPhotos/' + smallFileName, 'JPEG') im.close() try: timestamp = tags.get('EXIF DateTimeOriginal').__str__() date_obj = datetime.strptime(timestamp, '%Y:%m:%d %H:%M:%S') except: timestamp = 'No timestamp' date_obj = datetime.strptime("1900:1:1", '%Y:%m:%d') coords = GetGps(tags) GeoPoint = Point((coords[1], coords[0])) orientation = tags.get('Image Orientation').__str__() style = "" print(orientation) #Handle rotation in the Google earth popup if "180" in orientation: style = "style='-webkit-transform: rotate(-180deg);'" if "90 CCW" in orientation: style = "style='-webkit-transform: rotate(90deg);'" if "90 CW" in orientation: style = "style='-webkit-transform: rotate(90deg);'" kmlstyle = '#GPSphoto' kmlvis = '1' placeName = filename if coords[0] == 0: kmlstyle = '#noGPSphoto' kmlvis = '0' placeName = 'No GPS - ' + filename #return po = kml_doc.createElement('Placemark') name = kml_doc.createElement('name') name.appendChild(kml_doc.createTextNode(placeName)) snip = kml_doc.createElement('snippet') po.appendChild(snip) vis = kml_doc.createElement('visibility') vis.appendChild(kml_doc.createTextNode(kmlvis)) if timestamp != 'No timestamp': timespan = kml_doc.createElement('TimeStamp') taken = kml_doc.createElement('when') taken.appendChild(kml_doc.createTextNode(date_obj.isoformat())) timespan.appendChild(taken) po.appendChild(timespan) description = kml_doc.createElement('description') base = os.path.splitext(file_name)[0] ext = os.path.splitext(file_name)[1] file_name = base + ext.lower() cdata = "<h3>Date: " + timestamp + "</h3><h3>Latitude: " + str( coords[0] ) + "</h3><h3>Longitude: " + str( coords[1] ) + "</h3><img src='geoPhotos/" + smallFileName + "' width=600 " + style + " >" description.appendChild(kml_doc.createCDATASection(cdata)) po.appendChild(name) po.appendChild(description) styleurl = kml_doc.createElement('styleUrl') styleurl.appendChild(kml_doc.createTextNode(kmlstyle)) po.appendChild(styleurl) point = kml_doc.createElement('Point') amode = kml_doc.createElement('altitudeMode') amode.appendChild(kml_doc.createTextNode('clampToGround')) point.appendChild(amode) coordtext = kml_doc.createElement('coordinates') value = str(coords[1]) + "," + str(coords[0]) + "," + str(coords[2]) coordtext.appendChild(kml_doc.createTextNode(value)) point.appendChild(coordtext) po.appendChild(point) exists = 0 folder = kml_doc.getElementsByTagName('Folder')[0] for node in kml_doc.getElementsByTagName( 'Folder'): # visit every node <bar /> if node.getElementsByTagName("id"): name = node.getElementsByTagName("id")[0] if folderId == name.firstChild.data: folder = node exists = 1 if exists == 0: #Folder does not exist so we need to make one photoPath = splitall(file_name) level = 0 #remove the file name and set up folders in kml photoPath = photoPath[:-1] for part in photoPath: create = 1 for node in folder.getElementsByTagName( 'Folder'): # visit every node <bar /> #print(node.toxml()) if (node): name = node.getElementsByTagName("name")[0] if part == name.firstChild.data: #print('This folder Exists') folder = node create = 0 if (create): nextfolder = kml_doc.createElement('Folder') fname = kml_doc.createElement('name') folderid = kml_doc.createElement('id') folderid.appendChild(kml_doc.createTextNode(folderId)) fname.appendChild(kml_doc.createTextNode(part)) nextfolder.appendChild(folderid) nextfolder.appendChild(fname) folder.appendChild(nextfolder) folder = nextfolder folder.appendChild(po) if "EXIF DateTimeOriginal" in tags: DateTime = tags.get('EXIF DateTimeOriginal').__str__() date_obj = datetime.strptime(DateTime, '%Y:%m:%d %H:%M:%S') takenTime = int(time.mktime(date_obj.timetuple())) else: takenTime = 0 if "GPS GPSDate" in tags: GPSTime = tags.get('GPS GPSDate').__str__() else: GPSTime = 0 if "GPS GPSImgDirection" in tags: num, den = tags.get('GPS GPSImgDirection').__str__().split('/') photoBearing = int((float(num) / float(den))) gm = geomag.GeoMag() magDec = gm.GeoMag(coords[0], coords[1]).dec print(magDec) photoBearing = photoBearing + magDec else: photoBearing = None return Feature(geometry=GeoPoint, properties={ "visible": True, "bearing": photoBearing, "order": 0, "takenTime": takenTime, "GPSDate": GPSTime, "Path": smallFileName })
def run(self): """ the run method @return: """ startSequence = None seq = 0 self.api.log("started") self.api.setStatus('STARTED', 'running') gm = None computePeriod = 0.5 while not self.api.shouldStopMainThread(): if startSequence != self.startSequence: self.config = Config(self.api) try: computePeriod = float( self.api.getConfigValue('computePeriod', 0.5)) except: pass startSequence = self.startSequence if hasgeomag: wmm_filename = os.path.join(os.path.dirname(__file__), self.config.WMM_FILE) gm = geomag.GeoMag(wmm_filename) lastTime = time.time() gpsdata = {} computesVar = False computesWind = False try: gpsdata = self.api.getDataByPrefix('gps') if 'lat' in gpsdata and 'lon' in gpsdata and gm is not None: computesVar = True now = time.time() if now - self.variation_time > int( self.config.WMM_PERIOD ) or now < self.variation_time: variation = gm.GeoMag(gpsdata['lat'], gpsdata['lon']) self.variation_time = now self.variation_val = variation.dec self.api.addData(self.PATHGMM, self.variation_val) else: self.api.addData(self.PATHGMM, self.variation_val) except Exception: self.api.error(" error in calculation of magnetic Deviation") if 'windSpeed' in gpsdata: computesWind = True if gpsdata['windReference'] == 'R': if (self.calcTrueWind(gpsdata)): self.api.addData(self.PATHAWD, gpsdata['AWD']) self.api.addData(self.PATHTWD, gpsdata['TWD']) self.api.addData(self.PATHTWS, gpsdata['TWS']) self.api.addData(self.PATHTWA, gpsdata['TWA']) if computesVar or computesWind: stText = 'computing ' if computesVar: stText += 'variation ' if computesWind: stText += 'wind' self.api.setStatus('NMEA', stText) else: self.api.setStatus('STARTED', 'running') runNext = False # fetch from queue till next compute period while not runNext: now = time.time() if now < lastTime: # timeShift back runNext = True continue if ((now - lastTime) < computePeriod): waitTime = computePeriod - (now - lastTime) else: waitTime = 0.01 runNext = True seq, data = self.api.fetchFromQueue(seq, waitTime=waitTime, filter=self.FILTER) if len(data) > 0: for line in data: self.parseData(line)
#!/usr/bin/python import geomag gm = geomag.GeoMag("WMM.COF") #mag = gm.calc(43.411454, -80.472708) mag = gm.calc(-22.411454, -44.472708) print mag.dec
def run(self): """ the run method @return: """ lastnmea = 0 startSequence = None seq = 0 self.api.log("started") self.api.setStatus('STARTED', 'running') gm = None computePeriod = 0.5 source = 'more_nmea' while not self.api.shouldStopMainThread(): if startSequence != self.startSequence: self.outFilter = self.getConfigValue('FILTER_NMEA_OUT') if not (isinstance(self.outFilter, list)): self.outFilter = self.outFilter.split(',') try: source = self.api.getConfigValue("sourceName", None) computePeriod = float(self.getConfigValue('computePeriod')) startSequence = self.startSequence if hasgeomag: wmm_filename = os.path.join( os.path.dirname(__file__) + '/lib', self.getConfigValue('WMM_FILE')) gm = geomag.GeoMag(wmm_filename) except: self.api.error(" WMM-File " + wmm_filename + 'not found!') lastTime = time.time() gpsdata = {} self.WindData = [] computesVar = False computesWind = False try: gpsdata = self.api.getDataByPrefix('gps') if 'lat' in gpsdata and 'lon' in gpsdata and gm is not None: computesVar = True now = time.time() if now - self.variation_time > int( self.getConfigValue( 'WMM_PERIOD')) or now < self.variation_time: variation = gm.GeoMag(gpsdata['lat'], gpsdata['lon']) self.variation_time = now self.variation_val = variation.dec self.api.addData(self.PATHGMM, self.variation_val, source=source) else: self.api.addData(self.PATHGMM, self.variation_val, source=source) except Exception: self.api.error(" error in calculation of magnetic Variation") # fetch from queue till next compute period runNext = False while not runNext: now = time.time() if now < lastTime: # timeShift back runNext = True continue if ((now - lastTime) < computePeriod): waitTime = computePeriod - (now - lastTime) else: waitTime = 0.01 runNext = True seq, data = self.api.fetchFromQueue(seq, number=100, waitTime=waitTime, includeSource=True, filter=self.FILTER) if len(data) > 0: for line in data: if not source in line.source: # KEINE Auswertung von selbst erzeugten Daten!! self.parseData(line.data, source=source) gpsdata = self.api.getDataByPrefix('gps') if 'AWA' in self.WindData: computesApparentWind = True computesWind = True if (self.calcTrueWind(gpsdata)): self.api.addData(self.PATHAWA, gpsdata['AWA'], source=source) self.api.addData(self.PATHAWD, gpsdata['AWD'], source=source) self.api.addData(self.PATHAWS, gpsdata['AWS'], source=source) self.api.addData(self.PATHTWD, gpsdata['TWD'], source=source) self.api.addData(self.PATHTWS, gpsdata['TWS'], source=source) self.api.addData(self.PATHTWA, gpsdata['TWA'], source=source) if computesVar or computesWind: stText = 'computing ' if computesVar: stText += 'variation ' if computesWind: stText += 'wind' self.api.setStatus('NMEA', stText) else: self.api.setStatus('STARTED', 'running') if ((time.time() - lastnmea) > float( self.getConfigValue('NewNMEAPeriod'))): self.write_NMEA_records(gpsdata, source) self.receivedTags = [] lastnmea = now