def removeDuplilines(poly, type): p = [] t = [] stencil = 1000 sbar = SubProgressBar(maxval=len(poly)).start() found = False for i in range(len(poly)): for j in range(len(poly))[i + 2:i + 2 + stencil]: if isClose(poly[i], poly[j]): ia = (i + 1) % len(poly) ib = (j - 1) % len(poly) if isClose(poly[ia], poly[ib]): poly1, ptmp1, poly2, ptmp2, poly3 = np.split( poly, [i, ia, ib, j]) p.append(np.concatenate((poly1, poly3), axis=0)) if type == 1: t.append(1) p.append(poly2) t.append(1) found = True if found: break if found: break sbar.update(i) sbar.finish() if p == []: p.append(poly) t.append(type) return p, t
def removeDuplicates(poly,type): # /!\ does not work anymore found = True while found: i = 0; found = False while i < len(poly)-1: if isClose( poly[i],poly[i+1],size=10 ): found = True poly = np.delete(poly,i+1,0) i += 1 if len(poly) == 1: return [],0 elif len(poly) == 2: return [],0 #poly,0 else: if type != 0: if isClose( poly[len(poly)-1],poly[0],size=10 ): poly = np.delete(poly,len(poly)-1,0) if len(poly) < 3: return [],0 #poly,0 return poly,type
def getInS(file): # TODO: Read the whole header, for the time being head is copied # over # TODO: Read variables depending on type and on a list # ~~ Get all ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ core = getFileContent(file) # ~~ Parse head ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ icore = 0 while re.match(ken_header, core[icore]): # ~~> instruction FileType proc = re.match(asc_FileType, core[icore]) if proc: fileType = proc.group('type').lower() # ... more instruction coming ... icore += 1 head = core[0:icore] # /!\ icore starts the body # ~~ Parse body ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ # This is also fairly fast, so you might not need a progress bar poly = [] type = [] npoin = 0 while icore < len(core): # ~~> polygon head proc = re.match(var_1int, core[icore]) if not proc: print '\nCould not parse the following polyline header: ' + core[ icore] sys.exit() nrec = int(proc.group('number')) icore += 1 xyi = [] for irec in range(nrec): proc = re.match(var_2dbl, core[icore + irec]) if not proc: print '\nCould not parse the following polyline record: ' + core[ icore + irec + 1] sys.exit() xyi.append( [float(proc.group('number1')), float(proc.group('number2'))]) if xyi != []: cls = 0 if isClose(xyi[0], xyi[len(xyi) - 1], size=10): xyi.pop(len(xyi) - 1) cls = 1 poly.append(np.asarray(xyi)) type.append(cls) npoin += len(xyi) icore += nrec return head, fileType, npoin, poly, type
def putInS(file, head, fileType, poly, type=None): # ~~ Write head ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ if head != []: core = head else: core = [ ':FileType ' + fileType + ' ASCII EnSim 1.0', ':Application BlueKenue', ':Version 3.2.24', ':WrittenBy sebourban', ':CreationDate Thu, Dec 08, 2011 02:47 PM', ':Name ' + path.basename(file), ':AttributeUnits 1 m', ':EndHeader' ] # ~~ Look for closed lines ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ if type == None: type = [] for ip in poly: if isClose(ip[0], ip[len(ip) - 1]): type.append(True) else: type.append(False) # ~~ Write body ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ for ip, it in zip(poly, type): il = len(ip) if il != 0 and not isClose(ip[0], ip[len(ip) - 1]): il += it core.append(str(il) + ' 0') #TODO: you should use proper values if fileType == 'i2s': for xyi in ip: core.append(str(xyi[0]) + ' ' + str(xyi[1])) if il != len(ip): core.append(str(ip[0][0]) + ' ' + str(ip[0][1])) elif fileType == 'i3s': for xyi in ip: core.append( str(xyi[0]) + ' ' + str(xyi[1]) + ' ' + str(xyi[2])) if il != len(ip): core.append( str(ip[0][0]) + ' ' + str(ip[0][1]) + ' ' + str(ip[0][2])) # ~~ Put all ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ putFileContent(file, core) return
def putContent(self, fileName, head=[]): # ~~> file extension processing _, tail = path.splitext(fileName) if tail[1:] != self.fileType: if head != []: head = [ '\n'.join(head).replace(':FileType ' + self.fileType, ':FileType ' + tail[1:]) ] self.fileType = tail[1:] # ~~> write head if head != []: core = head else: core = [ ':FileType ' + self.fileType + ' ASCII EnSim 1.0', ':Application BlueKenue', ':Version 3.2.24', ':WrittenBy sebourban', ':CreationDate Thu, Dec 08, 2011 02:47 PM', ':Name ' + path.basename(fileName), #':AttributeName 1 level', #':AttributeType 1 float', #':AttributeUnits 1 m', ':EndHeader' ] # ~~> look for closed lines if self.type == []: for ip in self.poly: if isClose(ip[0][:2], ip[len(ip) - 1][:2]): self.type.append(1) else: self.type.append(0) # ~~> fill-up empty attributes if self.atrbut == {}: self.atrbut = {1: ['ArbitraryName1']} for _ in self.poly: self.atrbut[1].append(0) self.oatrbut = [1] # ~~> fill-up attribute names if head == []: for i, o in zip(range(len(self.oatrbut)), self.oatrbut): core.insert(-1, ':AttributeName ' + repr(i + 1) + ' ' + repr(o)) # ~~ Write body ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ for i, ip, iv, it in zip(range(len(self.poly)), self.poly, self.vals, self.type): il = len(ip) if il != 0 and not isClose(ip[0], ip[len(ip) - 1]): il += it line = repr(il) for o in self.oatrbut: line = line + ' ' + str(self.atrbut[o][i]) core.append(line) if self.fileType == 'i2s': for xyi in ip: core.append(repr(xyi[0]) + ' ' + repr(xyi[1])) if il != len(ip): core.append(repr(ip[0][0]) + ' ' + repr(ip[0][1])) elif self.fileType == 'i3s': if np.shape(iv)[1] == 0: for xyi in ip: core.append(repr(xyi[0]) + ' ' + repr(xyi[1]) + ' 0.0') if il != len(ip): core.append( repr(ip[0][0]) + ' ' + repr(ip[0][1]) + ' 0.0') else: for xyi, val in zip(ip, iv): core.append( repr(xyi[0]) + ' ' + repr(xyi[1]) + ' ' + ' '.join([repr(v) for v in val])) if il != len(ip): core.append( repr(ip[0][0]) + ' ' + repr(ip[0][1]) + ' ' + ' '.join([repr(v) for v in iv[0]])) # ~~ Put all ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ putFileContent(fileName, core)
def parseContent(self, fileName): # TODO: Read the whole header # ~~ Get all ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ core = getFileContent(fileName) # ~~ Parse head ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ icore = 0 self.atrbut = {} self.natrbut = 0 self.oatrbut = [] while re.match(ken_header, core[icore].strip()): # ~~> instruction FileType proc = re.match(asc_FileType, core[icore].strip()) if proc: self.fileType = proc.group('type').lower() # ~~> instruction AttributeName proc = re.match(asc_AttributeName, core[icore].strip()) if proc: self.natrbut += 1 if self.natrbut == int(proc.group('number')): self.oatrbut.append(proc.group('after').strip()) self.atrbut.update({self.oatrbut[-1]: []}) else: print '... Could not read the order of your Attributes:', core[ icore] sys.exit(1) # ... more instruction coming ... icore += 1 self.head = core[0:icore] # /!\ icore starts the body # ~~ Parse body ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ # This is also fairly fast, so you might not need a progress bar self.poly = [] self.vals = [] self.type = [] self.npoin = 0 while icore < len(core): if core[icore].strip() == '': icore += 1 continue # ~~> polygon head proc = re.match(var_1int, core[icore].strip()) if not proc: print '\nCould not parse the following polyline header: ' + core[ icore].strip() sys.exit(1) nrec = int(proc.group('number')) a = proc.group('after').strip().split() if len(a) != self.natrbut: if self.natrbut != 0: print '... Could not find the correct number of attribute:', core[ icore].strip(), ', ', self.natrbut, ' expected' sys.exit(1) else: self.natrbut = len(a) self.oatrbut = range(1, len(a) + 1) self.atrbut = dict([(i + 1, [a[i]]) for i in range(len(a))]) else: for i in range(len(self.oatrbut)): self.atrbut[self.oatrbut[i]].append(a[i]) xyi = [] val = [] icore += 1 for irec in range(nrec): nbres = core[icore + irec].strip().split() proc = re.match(var_1dbl, nbres[0]) if not proc: proc = re.match(var_1int, nbres[0]) if not proc: print '\nCould not parse the following polyline record: ' + core[ icore + irec].strip() sys.exit(1) nbres[0] = float(proc.group('number')) procd = re.match(var_1dbl, nbres[1]) proci = re.match(var_1int, nbres[1]) if procd: nbres[1] = float(procd.group('number')) elif proci: nbres[1] = float(procd.group('number')) xyi.append(nbres[:2]) val.append(nbres[2:]) if xyi != []: cls = 0 if isClose(xyi[0], xyi[len(xyi) - 1], size=10): xyi.pop(len(xyi) - 1) val.pop(len(val) - 1) cls = 1 self.poly.append(np.asarray(xyi, dtype=np.float)) self.vals.append(np.asarray(val, dtype=np.float)) self.type.append(cls) self.npoin += len(xyi) icore += nrec self.npoly = len(self.poly)