def extract(self): # Extract one KML folder out of the KML file logging.debug("extract") out = kmlfile() name = self.config.get('outfile') out.open(name) name = os.path.basename(name.replace(".kml", "")) out.header(name.capitalize()) out.styles(self.config.get('root') + '/styles.kml') logging.info("Opened %r for KML output" % self.config.get('outfile')) # Each inpuot file becomes a KML Folder in the output file for file in self.config.get('extract').split(','): pass try: self.file = open(self.config.get('infiles'), "r") logging.info("Opened %r for KML input" % file) except Exception as inst: logging.error("Couldn't open %r: %r" % (file, inst)) return cache = list() match = False start = False folder = False lines = self.file.readlines() for line in lines: m = re.search(".*Folder>.*", line) if m is not None: folder = True # out.folderStart() m = re.search(" *<Placemark>.*", line) if m is not None: if folder is True: folder = False cache.append(line) start = True continue start = True folder = False m = re.search(".*" + file + ".*", line) if m is not None: match = True m = re.search(" *</Placemark>.*", line) if m is not None: start = False if match is True: cache.append(line) out.write(cache) cache = list() match = False else: cache = list() match = False if start is True: cache.append(line) # out.folderEnd() self.file.close() out.footer()
def join(self): """Join multiple KML files into a singe KML file """ logging.debug("join") out = kmlfile() name = self.config.get('outfile') out.open(name) name = os.path.basename(name.replace(".kml", "")) title = self.config.get('title') if title is None: title = name.capitalize() out.header(title) out.styles(self.config.get('root') + '/styles.kml') logging.info("Opened %r for KML output" % self.config.get('outfile')) # Each inpuot file becomes a KML Folder in the output file for file in self.config.get('infiles').split(','): name = os.path.basename(file.replace(".kml", "")) out.folderStart(name) try: self.file = open(file, "r") logging.info("Opened %r for KML input" % file) except Exception as inst: logging.error("Couldn't open %r: %r" % (file, inst)) return start = False lines = self.file.readlines() for line in lines: m = re.search(" *<Placemark>", line) if m is not None: start = True m = re.search(" *<Folder>", line) if m is not None: start = True # End of the data we want m = re.search(" *</Document>", line) if m is not None: break if start is True: out.write(line) out.folderEnd() self.file.close() out.footer()
config.dump() def connect(self): self.db.connect('LocalRegion') def query(self, addr): return self.db.query(addr) #epdb.set_trace() dd = config(argv) fcall = plotcalls(dd) fcall.connect() kml = kmlfile() kml.open(dd.get('outfile')) kml.header("TLFPD Calls") calldata = open(dd.get('infile'), 'r') lines = calldata.readlines() fix = correct.correct() for line in lines: if line[1] == '#': continue index = line.find(' ') number = line[:index] street = line[index + 2:] street = street.replace("\n", '') street = street.strip() street = fix.alphaNumeric(street)
def split(self): """Split a KML file into separate files, one for each Folder""" logging.debug("spit") try: files = self.config.get('infiles') self.file = open(files, "r") except Exception as inst: logging.error("Couldn't open %r: %r" % (files, inst)) return # logging.debug a rotating character, so we know it's working rot = ("|", "/", "-", "\\", "*") k = 0 stdout = sys.stdout lines = self.file.readlines() name = False schema = False style = False header = list() kml = kmlfile() for line in lines: stdout.write(" Processing KML file: %s\r" % rot[k]) if k <= 3: k = k + 1 else: k = 0 if line[0] == '#': continue # if style is True: # header.append(line) # if schema is True: # header.append(line) # m = re.search(".*</Schema>.*", line) # if m is not None: # schema = False # pass # m = re.search(".*<Schema>.*", line) # if m is not None: # header.append(line) # schema = True # pass # m = re.search(".*</Style>.*", line) # if m is not None: # style = False # pass # m = re.search(".*<Style>.*", line) # if m is not None: # header.append(line) # style = True # pass m = re.search(".*</Folder>.*", line) if m is not None: kml.footer() continue m = re.search(".*<Folder>.*", line) if m is not None: name = True continue else: if self.file is not False: kml.write(line) if name is True: m = re.search(".*<name>.*</name>", line) if m is not None: start = line.find(">") + 1 end = line.rfind("<") folder = line[start:end] name = False logging.debug("Exporting KML Folder " + folder) kml.open("/tmp/" + folder + ".kml") kml.header(folder) kml.styles(self.config.get('root') + '/styles.kml') kml.write(header)