def add_intermediate_point_where_needed(cls, refs, offset=1): """Add intermediate point when crossing the grid of the ElevationGrid.""" if len(refs) < offset or not refs[offset - 1] in OSMCoord.coordDictionnary or WebotsObject.elevation is None: return refs previousXGridIndex, previousZGridIndex = \ WebotsObject.elevation.get_grid_indexes(OSMCoord.coordDictionnary[refs[offset - 1]].x, OSMCoord.coordDictionnary[refs[offset - 1]].z) newRefs = refs if offset >= len(refs): return newRefs for i in range(max(offset, 1), len(refs)): if not refs[i] in OSMCoord.coordDictionnary or not refs[i - 1] in OSMCoord.coordDictionnary: continue currentXGridIndex, currentZGridIndex = WebotsObject.elevation.get_grid_indexes(OSMCoord.coordDictionnary[refs[i]].x, OSMCoord.coordDictionnary[refs[i]].z) if not previousXGridIndex == currentXGridIndex and currentXGridIndex > 0 and previousXGridIndex > 0: d1x = math.fabs(WebotsObject.elevation.elevationArray[min(previousXGridIndex, currentXGridIndex)]['x'] - OSMCoord.coordDictionnary[refs[i]].x) d2x = math.fabs(WebotsObject.elevation.elevationArray[min(previousXGridIndex, currentXGridIndex)]['x'] - OSMCoord.coordDictionnary[refs[i - 1]].x) if d1x > 0.01 and d2x > 0.01: # need to add an intermediate point if none of the points are already very close from the border newCoordX = (d2x / (d1x + d2x)) * OSMCoord.coordDictionnary[refs[i]].x + \ (d1x / (d1x + d2x)) * OSMCoord.coordDictionnary[refs[i - 1]].x newCoordZ = (d2x / (d1x + d2x)) * OSMCoord.coordDictionnary[refs[i]].z + \ (d1x / (d1x + d2x)) * OSMCoord.coordDictionnary[refs[i - 1]].z newOSMID = OSMCoord.add_new_coord_to_list(newCoordX, newCoordZ) newRefs.insert(i, newOSMID) return cls.add_intermediate_point_where_needed(newRefs, i) if not previousZGridIndex == currentZGridIndex and currentZGridIndex > 0 and previousZGridIndex > 0: d1z = math.fabs(WebotsObject.elevation.elevationArray[max(previousZGridIndex, currentZGridIndex)]['z'] - OSMCoord.coordDictionnary[refs[i]].z) d2z = math.fabs(WebotsObject.elevation.elevationArray[max(previousZGridIndex, currentZGridIndex)]['z'] - OSMCoord.coordDictionnary[refs[i - 1]].z) if d1z > 0.01 and d2z > 0.01: # need to add an intermediate point if none of the points are already very close from the border newCoordX = (d2z / (d1z + d2z)) * OSMCoord.coordDictionnary[refs[i]].x +\ (d1z / (d1z + d2z)) * OSMCoord.coordDictionnary[refs[i - 1]].x newCoordZ = (d2z / (d1z + d2z)) * OSMCoord.coordDictionnary[refs[i]].z +\ (d1z / (d1z + d2z)) * OSMCoord.coordDictionnary[refs[i - 1]].z newOSMID = OSMCoord.add_new_coord_to_list(newCoordX, newCoordZ) newRefs.insert(i, newOSMID) return cls.add_intermediate_point_where_needed(newRefs, i) newCoordX, newCoordZ = get_intersection(OSMCoord.coordDictionnary[refs[i]].x, OSMCoord.coordDictionnary[refs[i]].z, OSMCoord.coordDictionnary[refs[i - 1]].x, OSMCoord.coordDictionnary[refs[i - 1]].z, WebotsObject.elevation.elevationArray[currentXGridIndex]['x'], WebotsObject.elevation.elevationArray[currentZGridIndex]['z'], WebotsObject.elevation.elevationArray[currentXGridIndex]['x'] + WebotsObject.elevation.xStep, WebotsObject.elevation.elevationArray[currentZGridIndex]['z'] + WebotsObject.elevation.zStep) if newCoordX is not None and newCoordZ is not None: # add point if intersect the triangle newOSMID = OSMCoord.add_new_coord_to_list(newCoordX, newCoordZ) newRefs.insert(i, newOSMID) return cls.add_intermediate_point_where_needed(newRefs, i + 2) previousXGridIndex = currentXGridIndex previousZGridIndex = currentZGridIndex return newRefs
def generate_tree_file(self, folder): """Generate the 'forest' file which contains the tree positions and is used by the 'Forest' PROTO.""" treeNumber = 0 polygon = [] xMin, xMax, zMin, zMax = OSMCoord.get_min_and_max_coord(self.ref) for ref in self.ref: polygon.append([ OSMCoord.coordDictionnary[ref].x, OSMCoord.coordDictionnary[ref].z ]) numberOfTree = int(round((xMax - xMin) * (zMax - zMin)) * self.density) if not os.path.exists(folder + '/forest'): os.makedirs(folder + '/forest') file = open(folder + '/forest/' + str(self.OSMID) + '.forest', 'w') for index in range(0, numberOfTree): x = random.uniform(xMin, xMax) z = random.uniform(zMin, zMax) y = 0 if WebotsObject.elevation is not None: y = WebotsObject.elevation.interpolate_height( -x + WebotsObject.xOffset, z + WebotsObject.zOffset) if Area.is_point_in_polygon(x, z, polygon) is True: treeNumber = treeNumber + 1 file.write("%.2f,%.2f,%.2f\n" % (x, y, z)) file.close() if treeNumber > 0: return 'forest/' + str(self.OSMID) + '.forest' else: os.remove('forest/' + str(self.OSMID) + '.forest') return None
def parse_node(self, node): """Parse a node element and create the appropriate OSMNode, OSMCoord and if needed Tree object.""" lat = node.attrib['lat'] lon = node.attrib['lon'] osmId = node.attrib['id'] tags = self.get_tags(node) OSMNode.add(osmId, lon, lat, tags) OSMCoord.add(osmId, lon, lat) if 'natural' in tags: tree = Tree() if 'height' in tags: tree.height = float(tags['height']) if 'leaf_type' in tags: tree.leafType = tags['leaf_type'] if 'diameter_crown' in tags: tree.radius = tags['diameter_crown'] / 2.0 tree.coord = OSMCoord.coordDictionnary[osmId] Tree.list.append(tree)
maxlat=maxlat, maxlon=maxlon) WebotsObject.elevation = elevation # parse OSM file parser = Parser() parser.parse_file(options.inFile, options.disableMultipolygonBuildings) Road.initialize_speed_limit(parser.country) print(" * OSM filed parsed") if options.enable3D and elevation is not None: add_height_to_coordinates( elevation) # important to do it before 'center_coordinates' xOffset, zOffset = OSMCoord.center_coordinates(minlat=minlat, minlon=minlon, maxlat=maxlat, maxlon=maxlon) WebotsObject.xOffset = xOffset WebotsObject.zOffset = zOffset # From now we are in local coordinates system and not earth coordinates system anymore # print all the Webots objects if not options.noRoads: Road.process() Road.export(outputFile) print(" * " + str(len(Road.roads)) + " roads generated") print(" * " + str(len(Road.crossroads)) + " crossroads generated") if not options.noBuildings: Building.export(outputFile) print(" * " + str(len(Building.list)) + " buildings generated")
def draw_area(file, refs, red=1, green=0, blue=0, defName="", transparency=0.0, texture="", drawFlat=False, verticalOffset=0.0): """Draw an area.""" if len(refs) < 3: return if not defName == "": file.write("DEF " + defName + " " + "Transform {\n") else: file.write("Transform {\n") file.write(" translation %f 0 %f\n" % (OSMCoord.coordDictionnary[refs[0]].x, OSMCoord.coordDictionnary[refs[0]].z)) file.write(" children [\n") file.write(" Shape {\n") file.write(" appearance Appearance {\n") file.write(" material Material {\n") file.write(" diffuseColor " + str(red) + " " + str(green) + " " + str(blue) + "\n") if transparency > 0: file.write(" transparency " + str(transparency) + "\n") file.write(" }\n") if not texture == "": file.write(" texture ImageTexture {\n") file.write(" url [\n") file.write(" \"" + texture + "\"\n") file.write(" ]\n") file.write(" }\n") xMin, xMax, zMin, zMax = OSMCoord.get_min_and_max_coord(refs) scale = max(abs(round(xMax - xMin)), abs(round(zMax - zMin))) file.write(" textureTransform TextureTransform {\n") file.write(" scale %.2f %.2f\n" % (scale, scale)) file.write(" }\n") file.write(" }\n") file.write(" geometry IndexedFaceSet {\n") file.write(" coord Coordinate {\n") file.write(" point [\n") if drawFlat: height = 0 for ref in refs: if ref in OSMCoord.coordDictionnary: height = height + OSMCoord.coordDictionnary[ref].y height = height / len(refs) for ref in refs: if ref in OSMCoord.coordDictionnary: if drawFlat: file.write( " %.2f %.2f %.2f,\n" % (OSMCoord.coordDictionnary[ref].x - OSMCoord.coordDictionnary[refs[0]].x, height + verticalOffset, OSMCoord.coordDictionnary[ref].z - OSMCoord.coordDictionnary[refs[0]].z)) else: file.write( " %.2f %.2f %.2f,\n" % (OSMCoord.coordDictionnary[ref].x - OSMCoord.coordDictionnary[refs[0]].x, OSMCoord.coordDictionnary[ref].y + verticalOffset, OSMCoord.coordDictionnary[ref].z - OSMCoord.coordDictionnary[refs[0]].z)) else: print("Warning: node " + str(ref) + " not referenced.") file.write(" ]\n") file.write(" }\n") if Area.are_references_clockwise(refs) is False: file.write(" ccw FALSE\n") file.write(" coordIndex [\n") for i in range(0, len(refs)): file.write(" " + str(i) + "\n") file.write(" -1\n") file.write(" ]\n") file.write(" }\n") file.write(" castShadows FALSE\n") file.write(" }\n") file.write(" ]\n") file.write("}\n")