def getMissingHgtFiles(self): """ Returns the list of missing .hgt files """ latIntervals = self.latIntervals lonIntervals = self.lonIntervals missingFiles = [] for latInterval in latIntervals: # latitude of the lower-left corner of the .hgt tile _lat = math.floor(latInterval[0]) for lonInterval in lonIntervals: # longitude of the lower-left corner of the .hgt tile _lon = math.floor(lonInterval[0]) hgtFileName = os.path.join(self.terrainDir, Terrain.getHgtFileName(_lat, _lon)) # check if the .hgt file exists if not os.path.isfile(hgtFileName): missingFiles.append(hgtFileName) return missingFiles
def buildTerrain(self, verts, indices, heightOffset): """ The method fills verts and indices lists with values verts is a list of vertices indices is a list of tuples; each tuple is composed of 3 indices of verts that define a triangle Returns the minimal height """ latIntervals = self.latIntervals lonIntervals = self.lonIntervals size = self.terrainSize makeQuads = (self.terrainPrimitiveType == "quad") minHeight = 32767 vertsCounter = 0 # we have an extra row for the first latitude interval firstLatInterval = 1 # initialize the array of vertCounter values lonIntervalVertsCounterValues = [] for lonInterval in lonIntervals: lonIntervalVertsCounterValues.append(None) for latInterval in latIntervals: # latitude of the lower-left corner of the .hgt tile _lat = math.floor(latInterval[0]) # vertical indices that limit the active .hgt tile area y1 = math.floor( size * (latInterval[0] - _lat) ) y2 = math.ceil( size * (latInterval[1] - _lat) ) + firstLatInterval - 1 # we have an extra column for the first longitude interval firstLonInterval = 1 for lonIntervalIndex,lonInterval in enumerate(lonIntervals): # longitude of the lower-left corner of the .hgt tile _lon = math.floor(lonInterval[0]) # horizontal indices that limit the active .hgt tile area x1 = math.floor( size * (lonInterval[0] - _lon) ) + 1 - firstLonInterval x2 = math.ceil( size * (lonInterval[1] - _lon) ) xSize = x2-x1 filepath = os.path.join(self.terrainDir, Terrain.getHgtFileName(_lat, _lon)) with gzip.open(filepath, "rb") as f: for y in range(y2, y1-1, -1): # set the file object position at y, x1 f.seek( 2*((size-y)*(size+1) + x1) ) for x in range(x1, x2+1): lat = _lat + y/size lon = _lon + x/size xy = self.projection.fromGeographic(lat, lon) # read two bytes and convert them buf = f.read(2) # ">h" is a signed two byte integer z = struct.unpack('>h', buf)[0] if z==self.voidValue: z = self.voidSubstitution if heightOffset is None and z<minHeight: minHeight = z # add a new vertex to the verts array verts.append([xy[0], xy[1], z]) if not firstLatInterval and y==y2: topNeighborIndex = lonIntervalVertsCounterValues[lonIntervalIndex] + x - x1 if x!=x1: if makeQuads: indices.append((vertsCounter, topNeighborIndex, topNeighborIndex-1, vertsCounter-1)) else: # self.primitiveType == "triangle" indices.append((vertsCounter-1, topNeighborIndex, topNeighborIndex-1)) indices.append((vertsCounter, topNeighborIndex, vertsCounter-1)) elif not firstLonInterval: leftNeighborIndex = prevLonIntervalVertsCounter - (y2-y1)*(prevXsize+1) leftTopNeighborIndex = topNeighborIndex-prevYsize*(x2-x1+1)-1 if makeQuads: indices.append((vertsCounter, topNeighborIndex, leftTopNeighborIndex, leftNeighborIndex)) else: # self.primitiveType == "triangle" indices.append((leftNeighborIndex, topNeighborIndex, leftTopNeighborIndex)) indices.append((vertsCounter, topNeighborIndex, leftNeighborIndex)) elif not firstLonInterval and x==x1: if y!=y2: leftNeighborIndex = prevLonIntervalVertsCounter - (y-y1)*(prevXsize+1) topNeighborIndex = vertsCounter-xSize-1 leftTopNeighborIndex = leftNeighborIndex-prevXsize-1 if makeQuads: indices.append((vertsCounter, topNeighborIndex, leftTopNeighborIndex, leftNeighborIndex)) else: # self.primitiveType == "triangle" indices.append((leftNeighborIndex, topNeighborIndex, leftTopNeighborIndex)) indices.append((vertsCounter, topNeighborIndex, leftNeighborIndex)) elif x>x1 and y<y2: topNeighborIndex = vertsCounter-xSize-1 leftTopNeighborIndex = vertsCounter-xSize-2 if makeQuads: indices.append((vertsCounter, topNeighborIndex, leftTopNeighborIndex, vertsCounter-1)) else: # self.primitiveType == "triangle" indices.append((vertsCounter-1, topNeighborIndex, leftTopNeighborIndex)) indices.append((vertsCounter, topNeighborIndex, vertsCounter-1)) vertsCounter += 1 if firstLonInterval: # we don't have an extra column anymore firstLonInterval = 0 # remembering vertsCounter value prevLonIntervalVertsCounter = vertsCounter - 1 lonIntervalVertsCounterValues[lonIntervalIndex] = prevLonIntervalVertsCounter - xSize # remembering xSize prevXsize = xSize if firstLatInterval: firstLatInterval = 0 # remembering ySize prevYsize = y2-y return minHeight if heightOffset is None else heightOffset