def appendNewWay(coords, intersects, osmXml): way = etree.Element('way', visible='true', id=str(newOsmId('way'))) firstNid = 0 for i, coord in enumerate(coords): if i == 0: continue # the first and last coordinate are the same node = appendNewNode(coord, osmXml) if i == 1: firstNid = node.get('id') way.append(etree.Element('nd', ref=node.get('id'))) # Check each way segment for intersecting nodes int_nodes = {} try: line = LineString([coord, coords[i+1]]) except IndexError: line = LineString([coord, coords[1]]) for idx, c in enumerate(intersects): if line.buffer(0.0000015).contains(Point(c[0], c[1])) and c not in coords: t_node = appendNewNode(c, osmXml) for n in way.iter('nd'): if n.get('ref') == t_node.get('id'): break else: int_nodes[t_node.get('id')] = Point(c).distance(Point(coord)) for n in sorted(int_nodes, key=lambda key: int_nodes[key]): # add intersecting nodes in order way.append(etree.Element('nd', ref=n)) way.append(etree.Element('nd', ref=firstNid)) # close way osmXml.append(way) return way
def divide_polygon_for_intersection(self, segments): """ Generates multiple polygons based on cutting the fracture faces by line segments. """ R = self.build_rotation_matrix() fracture_poly_list = [] # face_poly_list = [] for point in self.points: rot_point = np.linalg.solve(R, point - self.center) fracture_poly_list.append(rot_point[:2]) seg_rot = [] for seg in segments: p1 = seg[0] p2 = seg[1] vec = p2 - p1 p1 = p1 - 100.0 * vec p2 = p2 + 100.0 * vec p1_rot = np.linalg.solve(R, p1 - self.center) p2_rot = np.linalg.solve(R, p2 - self.center) line = LineString((p1_rot[:2], p2_rot[:2])) dilated = line.buffer(1.0e-10) seg_rot.append(dilated) fracture_poly = Polygon(fracture_poly_list) divided_polygons = fracture_poly.difference(seg_rot[0]) return (divided_polygons, fracture_poly)
def cut_line(cut_point, line, eps_mult=1e2): dist = line.project(cut_point) point = line.interpolate(dist) eps = line.distance(point) * eps_mult coords = list(line.coords) if point.coords[0] in coords: i = coords.index(point.coords[0]) if i == 0: return LineString(), line if i == len(coords) - 1: return line, LineString() start_segment = LineString(coords[:i + 1]) end_segment = LineString(coords[i:]) return start_segment, end_segment for i, p in enumerate(coords[:-1]): line_segment = LineString([coords[i], coords[i + 1]]) line_segment_buffer = line_segment.buffer(eps, resolution=1) if line_segment_buffer.contains(point): start_segment = LineString(coords[:i + 1] + [point]) end_segment = LineString([point] + coords[i + 1:]) return start_segment, end_segment raise Exception('point not found in line, consider raising eps_mult')
def SpatialToplogy (Spatial_A,Spatial_B): if Spatial_A[4] == 'Point' and Spatial_B[4]== 'Point': Point_0=Point(Spatial_A[0],Spatial_A[1]) Point_1=Point(Spatial_B[0],Spatial_B[1]) #Point to point relationships if Point_0.equals(Point_1): return 'Point1 equals Point2' if Point_0.within(Point_1.buffer(2)): return 'Point1 lies within a buffer of 2 m from Point2' if Point_0.overlaps(Point_1): return 'Point1 overlaps Point2' #if Point_0.disjoint(Point_1): return 'Point1 disjoint Point2' #Point to line relationships if Spatial_A[4] == 'Point' and Spatial_B[4]== 'Line': Point_0=Point(Spatial_A[0],Spatial_A[1]) Line_0=LineString([(Spatial_B[0],Spatial_B[1]),(Spatial_B[2],Spatial_B[3])]) if Point_0.touches(Line_0):return 'Point1 touches Line1' if Point_0.within(Line_0.buffer(2)):return 'Point1 lies within a buffer of 2 m from L1' #Point to polygon relationships if Spatial_A[4] == 'Point' and Spatial_B[4]== 'Polygon': Point_0=Point(Spatial_A[0],Spatial_A[1]) Polygon_0=Polygon([(Spatial_B[0],Spatial_B[1]),(Spatial_B[2],Spatial_B[1]),(Spatial_B[2],Spatial_B[3]),(Spatial_B[0],Spatial_B[3])]) if Point_0.touches(Polygon_0):return 'Point1 touches Polygon1' if Point_0.within(Polygon_0):return'Point1 lies within Polygon1' if Point_0.overlaps(Polygon_0):return 'Point1 lies overlaps Polygon1' #Line to line relationships if Spatial_A[4]=='Line' and Spatial_B[4]=='Line': Line_0=LineString([(Spatial_A[0],Spatial_A[1]),(Spatial_A[2],Spatial_A[3])]) Line_1=LineString([(Spatial_B[0],Spatial_B[1]),(Spatial_B[2],Spatial_B[3])]) if Line_0.equals(Line_1):return 'Line0 equals Line1' if Line_0.touches(Line_1):return 'Line0 touches Line1' if Line_0.crosses(Line_1):return 'Line0 crosses Line1' if Line_0.within(Line_1.buffer(2)):return 'Line0 lies within a buffer of 2 m Line1' if Line_0.overlaps(Line_1):return 'Line0 overlaps Line1' #Line to polygon relationships if Spatial_A[4]=='Line' and Spatial_B[4]=='Polygon': Line_0=LineString([(Spatial_A[0],Spatial_A[1]),(Spatial_A[2],Spatial_A[3])]) Polygon_0=Polygon([(Spatial_B[0],Spatial_B[1]),(Spatial_B[2],Spatial_B[1]),(Spatial_B[2],Spatial_B[3]),(Spatial_B[0],Spatial_B[3])]) if Line_0.touches(Polygon_0):return 'Line0 touches Polygon1' if Line_0.crosses(Polygon_0):return 'Line0 crosses Polygon1' if Line_0.within(Polygon_0):return 'Line0 lies within Polygon1' #Polygon to Polygon relationships if Spatial_A[4]=='Polygon' and Spatial_B[4]=='Polygon': Polygon_0=Polygon([(Spatial_A[0],Spatial_A[1]),(Spatial_A[2],Spatial_A[1]),(Spatial_A[2],Spatial_A[3]),(Spatial_A[0],Spatial_A[3])]) Polygon_1=Polygon([(Spatial_B[0],Spatial_B[1]),(Spatial_B[2],Spatial_B[1]),(Spatial_B[2],Spatial_B[3]),(Spatial_B[0],Spatial_B[3])]) if Polygon_0.touches(Polygon_1):return 'Polygon touches Polygon1' if Polygon_0.equals(Polygon_1):return 'Polygon0 equals Polygon1' if Polygon_0.within(Polygon_1):return 'Polygon lies within Polygon1' if Polygon_0.within(Polygon_1.buffer(2)):return 'Polygon lies within a buffer of 2m Polygon1'
def line_to_poly(raw_line, distance=0.05): """ Line in format [(), ..., ()] to Polygon with 2*distance width Args: raw_line (list): Connected dots distance (Optional[float]): width of polygon = 2*distance """ line = LineString(raw_line) return Polygon(line.buffer(distance, cap_style=2, join_style=2))
def create_s(): c1 = create_circle(0, 1.03, 1, 20, 270-15) c2 = create_circle(0, -1.03, 1, 200, 450-15) s = LineString(c1 + list(reversed(c2))) s = s.buffer(0.4) g = GCode.from_geometry(s, 0, 0) g = g.scale_to_fit(6, 8) g = g.move(3, 4, 0.5, 0.5) g1 = g.depth(0.2, -0.3) g2 = g.depth(0.2, -0.6) g = HEADER + g1 + g2 + FOOTER g.save('sophia.nc') im = g.render(0, 0, 6, 8, 96) im.write_to_png('sophia.png')
def WayToPoly(wayId, ways, nodes): wayData = ways[wayId] wayNodes = wayData[0] if wayNodes[0] == wayNodes[-1]: #Close polygon tags = wayData[1] pts = [] for nid in wayNodes: if int(nid) not in nodes: print "Warning: missing node", nid continue pts.append(nodes[int(nid)][0]) #Require at least 3 points if len(pts) < 3: return None poly = Polygon(pts) if not poly.is_valid: print "Warning: polygon is not valid" print explain_validity(poly) poly = poly.buffer(0) return poly else: #Unclosed way tags = wayData[1] pts = [] for nid in wayNodes: if int(nid) not in nodes: print "Warning: missing node", nid continue pts.append(nodes[int(nid)][0]) line = LineString(pts) if not line.is_valid: print "Warning: polygon is not valid" print explain_validity(line) line = line.buffer(0) return line return None
def create_frames(mapfile): # increment in steps of 3 units from 1 to 35 min_buffer = 1 max_buffer = 35 step = 3 # create a line line = LineString([(0, 0), (100, 100), (0, 200), (200, 200), (300, 100), (100, 0)]) # set the map extent to this line mapfile["extent"] = " ".join(map(str, line.buffer(max_buffer*2).bounds)) all_images = [] # create expanding buffers for dist in range(min_buffer, max_buffer, step): all_images.append(create_frame(mapfile, line, dist)) # create shrinking buffers for dist in range(max_buffer, min_buffer, -step): all_images.append(create_frame(mapfile, line, dist)) return all_images
def lines_multiplot(list_in, list_out, out_name): line_in = LineString(list_in) line_out = LineString(list_out) fig = pyplot.figure(1, figsize=(10, 4), dpi=140) fig.suptitle('Results of the Douglas-Peucker algorithm', fontsize=14, fontweight='bold') ax = fig.add_subplot(111) x1, y1 = line_in.xy x2, y2 = line_out.xy ax.plot(x1, y1, 'r^--',x2,y2, 'bs-' ,solid_capstyle='round', zorder=1, linewidth=1.5) ax.legend(['original route','simplified route'], loc='upper center', bbox_to_anchor=(0.5, -0.1), fancybox=True, shadow=True, ncol=2) dilated = line_in.buffer(0.005) patch1 = PolygonPatch(dilated, fc=BLUE, ec=BLUE, alpha=0.5, zorder=2) ax.add_patch(patch1) ax.tick_params(direction='out', length=2, width=0.8, pad = 0.05) box = ax.get_position() ax.set_position([box.x0, box.y0 + box.height * 0.1, box.width, box.height * 0.9]) #make some space around the plots start, end = ax.get_xlim() xrange = [start-0.005, end+0.005] ax.set_xlim(*xrange) ax.xaxis.set_tick_params(labelsize=8) start, end = ax.get_ylim() yrange = [start-0.005, end+0.005] ax.set_ylim(*yrange) ax.set_aspect(1) ax.yaxis.set_tick_params(labelsize=8) #pyplot.show() fig.savefig(str(out_name)+'.png') # save the figure to file
def createConvexPath(self, pair): #pr = cProfile.Profile() #pr2 = cProfile.Profile() print pair[1] odPointsList = ((pair[0][0].x, pair[0][0].y), (pair[0][1].x, pair[0][1].y)) #st_line = LineString(odPointsList) labeledObstaclePoly = [] totalConvexPathList = {} dealtArcList = {} totalConvexPathList[odPointsList] = LineString(odPointsList) terminate = 0 idx_loop1 = 0 #sp_l_set = [] time_loop1 = 0 time_contain2 = 0 time_crossingDict = 0 time_convexLoop = 0 time_impedingArcs = 0 time_spatialFiltering = 0 time_loop1_crossingDict = 0 time_buildConvexHulls = 0 while terminate == 0: t1s = time.time() idx_loop1 += 1 t6s = time.time() #w = shapefile.Writer(shapefile.POLYLINE) #w.field('nem') #for line in totalConvexPathList: #w.line(parts=[[ list(x) for x in line ]]) #w.record('ff') #w.save(self.path + "graph_" + str(idx_loop1) + self.version_name) totalGrpah = self.createGraph(totalConvexPathList.keys()) spatial_filter_n = networkx.dijkstra_path(totalGrpah, odPointsList[0], odPointsList[1]) spatial_filter = [] for i in xrange(len(spatial_filter_n)-1): spatial_filter.append([spatial_filter_n[i], spatial_filter_n[i+1]]) #w = shapefile.Writer(shapefile.POLYLINE) #w.field('nem') #for line in spatial_filter: #w.line(parts=[[ list(x) for x in line ]]) #w.record('ff') #w.save(self.path + "spatial Filter_" + str(idx_loop1) + self.version_name) #sp_length = 0 #for j in spatial_filter: #sp_length += LineString(j).length #sp_l_set.append(sp_length) crossingDict = defaultdict(list) for line in spatial_filter: Line = LineString(line) for obs in self.obstaclesPolygons: if Line.crosses(obs): if obs not in labeledObstaclePoly: labeledObstaclePoly.append(obs) crossingDict[tuple(line)].append(obs) t6e = time.time() time_spatialFiltering += t6e - t6s if len(crossingDict.keys()) == 0: terminate = 1 continue else: t7s = time.time() for tLine in crossingDict.keys(): #cLine = list(tLine) if dealtArcList.has_key(tLine): try: del totalConvexPathList[tLine] except: del totalConvexPathList[(tLine[1], tLine[0])] continue else: dealtArcList[tLine] = LineString(list(tLine)) try: del totalConvexPathList[tLine] except: del totalConvexPathList[(tLine[1], tLine[0])] containingObs = [] for obs in crossingDict[tLine]: convexHull = self.createConvexhull(obs, tLine) self.splitBoundary(totalConvexPathList, convexHull) convexHull = self.createConvexhull(obs, odPointsList) self.splitBoundary(totalConvexPathList, convexHull) convexHull2 = self.createConvexhull(obs) if convexHull2.contains(Point(tLine[0])): containingObs.append(obs) elif convexHull2.contains(Point(tLine[1])): containingObs.append(obs) if len(containingObs) != 0: #SPLIT subConvexPathList = {} vi_obs = MultiPolygon([x for x in containingObs]) containedLineCoords = list(tLine) fromX = containedLineCoords[0][0] fromY = containedLineCoords[0][1] toX = containedLineCoords[1][0] toY = containedLineCoords[1][1] fxA = (fromY - toY) / (fromX - toX) fxB = fromY - (fxA * fromX) minX = vi_obs.bounds[0] maxX = vi_obs.bounds[2] split_line = LineString([(min(minX, fromX, toX), fxA * min(minX, fromX, toX) + fxB), (max(maxX, fromX, toX), fxA * max(maxX, fromX, toX) + fxB)]) for obs in containingObs: s1, s2 = self.splitPolygon(split_line, obs) dividedObsPoly = [] #to deal with multipolygon a = s1.intersection(obs) b = s2.intersection(obs) if a.type == "Polygon": dividedObsPoly.append(a) else: for o in a.geoms: if o.type == "Polygon": dividedObsPoly.append(o) if b.type == "Polygon": dividedObsPoly.append(b) else: for o2 in b.geoms: if o2.type == "Polygon": dividedObsPoly.append(o2) for obs2 in dividedObsPoly: for pt in tLine: convexHull = self.createConvexhull(obs2, [pt]) self.splitBoundary(subConvexPathList, convexHull) subVertices = [] for line in subConvexPathList: subVertices.extend(line) subVertices = list(set(subVertices)) containingObsVertices = [] for obs in containingObs: containingObsVertices.extend(list(obs.exterior.coords)) subVertices = [x for x in subVertices if x in containingObsVertices] deleteList = [] for line in subConvexPathList: chk_cross = 0 for obs in containingObs: if subConvexPathList[line].crosses(obs): chk_cross = 1 if chk_cross == 1: deleteList.append(line) for line in deleteList: del subConvexPathList[line] #subConvexPathList.remove(line) pairList = [] for i in range(len(subVertices)): for j in range(i+1, len(subVertices)): pairList.append((subVertices[i], subVertices[j])) for i in pairList: Line = LineString(i) chk_cross = 0 for obs in containingObs: if Line.crosses(obs): chk_cross = 1 elif Line.within(obs): chk_cross = 1 if chk_cross == 0: subConvexPathList[i] = Line #subConvexPathList.append(i) buffer_st_line = split_line.buffer(0.1) deleteList = [] for line in subConvexPathList: if buffer_st_line.contains(subConvexPathList[line]): deleteList.append(line) for line in deleteList: if subConvexPathList.has_key(line): del subConvexPathList[line] #subConvexPathList = [x for x in subConvexPathList if x not in deleteList] for line in subConvexPathList: if not totalConvexPathList.has_key(line): if not totalConvexPathList.has_key((line[1],line[0])): totalConvexPathList[line] = subConvexPathList[line] #if line not in totalConvexPathList: #if [line[1], line[0]] not in totalConvexPathList: #totalConvexPathList.append(line) #w = shapefile.Writer(shapefile.POLYLINE) #w.field('nem') #for line in totalConvexPathList: #w.line(parts=[[ list(x) for x in line ]]) #w.record('ff') #w.save(self.path + "graph2_" + str(idx_loop1) + self.version_name) t7e = time.time() time_loop1_crossingDict += t7e - t7s #new lines labeled_multyPoly = MultiPolygon([x for x in labeledObstaclePoly]) convexHull = self.createConvexhull(labeled_multyPoly, odPointsList) self.splitBoundary(totalConvexPathList, convexHull) #new lines end #impededPathList t5s = time.time() impededPathList = {} for line in totalConvexPathList: for obs in labeledObstaclePoly: if totalConvexPathList[line].crosses(obs): impededPathList[line] = totalConvexPathList[line] break t5e = time.time() time_impedingArcs += t5e - t5s for line in impededPathList: del totalConvexPathList[line] terminate2 = 0 idx_loop2 = 0 t1e = time.time() time_loop1 += t1e - t1s #w = shapefile.Writer(shapefile.POLYGON) #w.field('net') #for obs in labeledObstaclePoly: #w.poly(parts=[[list(x) for x in list(obs.exterior.coords)]]) #w.record('ff') #w.save(self.path + "obs"+ str(idx_loop1) + "_" + self.version_name) while terminate2 == 0: idx_loop2 += 1 deleteList = [] crossingDict = defaultdict(list) for line in dealtArcList: if impededPathList.has_key(line): del impededPathList[line] elif impededPathList.has_key((line[1], line[0])): del impededPathList[(line[1],line[0])] t3s = time.time() #pr.enable() for line in impededPathList: for obs in labeledObstaclePoly: if impededPathList[line].crosses(obs): crossingDict[line].append(obs) t3e = time.time() time_crossingDict += t3e - t3s #at this point, impededArcList should be emptied, as it only contains crossing arcs, and all of them #should be replaced by convex hulls. for line in crossingDict: del impededPathList[line] for line in impededPathList: if not totalConvexPathList.has_key(line): totalConvexPathList[line] = impededPathList[line] impededPathList = {} if len(crossingDict.keys()) == 0: terminate2 = 1 continue else: #w = shapefile.Writer(shapefile.POLYLINE) #w.field('nem') #for line in crossingDict: #w.line(parts=[[ list(x) for x in line ]]) #w.record('ff') #w.save(self.path + "crossingDict_" + str(idx_loop1) + "_"+ str(idx_loop2) +"_"+ self.version_name) t4s = time.time() for tLine in crossingDict.keys(): dealtArcList[tLine] = crossingDict[tLine] containingObs = [] for obs in crossingDict[tLine]: chk_contain = 0 convexHull2 = self.createConvexhull(obs) if convexHull2.contains(Point(tLine[0])): containingObs.append(obs) chk_contain = 1 elif convexHull2.contains(Point(tLine[1])): containingObs.append(obs) chk_contain = 1 if chk_contain == 0: t10s = time.time() convexHull = self.createConvexhull(obs, tLine) self.splitBoundary(impededPathList, convexHull) t10e = time.time() time_buildConvexHulls += t10e - t10s if len(containingObs) != 0: #SPLIT #print "SPLIT" t2s = time.time() subConvexPathList = {} vi_obs = MultiPolygon([x for x in containingObs]) containedLineCoords = tLine fromX = containedLineCoords[0][0] fromY = containedLineCoords[0][1] toX = containedLineCoords[1][0] toY = containedLineCoords[1][1] fxA = (fromY - toY) / (fromX - toX) fxB = fromY - (fxA * fromX) minX = vi_obs.bounds[0] maxX = vi_obs.bounds[2] split_line = LineString([(min(minX, fromX, toX), fxA * min(minX, fromX, toX) + fxB), (max(maxX, fromX, toX), fxA * max(maxX, fromX, toX) + fxB)]) for obs in containingObs: s1, s2 = self.splitPolygon(split_line, obs) dividedObsPoly = [] #to deal with multipolygon a = s1.intersection(obs) b = s2.intersection(obs) if a.type == "Polygon": dividedObsPoly.append(a) else: for o in a.geoms: if o.type == "Polygon": dividedObsPoly.append(o) if b.type == "Polygon": dividedObsPoly.append(b) else: for o2 in b.geoms: if o2.type == "Polygon": dividedObsPoly.append(o2) for obs2 in dividedObsPoly: for pt in tLine: convexHull = self.createConvexhull(obs2, [pt]) self.splitBoundary(subConvexPathList, convexHull) subVertices = [] for line in subConvexPathList: subVertices.extend(line) subVertices = list(set(subVertices)) containingObsVertices = [] for obs in containingObs: containingObsVertices.extend(list(obs.exterior.coords)) subVertices = [x for x in subVertices if x in containingObsVertices] deleteList = [] for line in subConvexPathList: chk_cross = 0 for obs in containingObs: if subConvexPathList[line].crosses(obs): chk_cross = 1 if chk_cross == 1: deleteList.append(line) for line in deleteList: del subConvexPathList[line] pairList = [] for i in range(len(subVertices)): for j in range(i+1, len(subVertices)): pairList.append((subVertices[i], subVertices[j])) for i in pairList: Line = LineString(list(i)) chk_cross = 0 for obs in containingObs: if Line.crosses(obs): chk_cross = 1 elif Line.within(obs): chk_cross = 1 if chk_cross == 0: subConvexPathList[i] = Line buffer_st_line = split_line.buffer(0.1) deleteList = [] for line in subConvexPathList: if buffer_st_line.contains(subConvexPathList[line]): deleteList.append(line) for line in deleteList: del subConvexPathList[line] for line in subConvexPathList: if not impededPathList.has_key(line): if not impededPathList.has_key((line[1], line[0])): impededPathList[line] = subConvexPathList[line] t2e = time.time() time_contain2 += t2e - t2s #pr.disable() for line in dealtArcList: if impededPathList.has_key(line): del impededPathList[line] #impededPathList = [x for x in impededPathList if x not in dealtArcList] t4e = time.time() time_convexLoop += t4e - t4s #end of else #w = shapefile.Writer(shapefile.POLYLINE) #w.field('nem') #for line in impededPathList: #w.line(parts=[[ list(x) for x in line ]]) #w.record('ff') #w.save(self.path + "After_graph_" + str(idx_loop1) + "_"+ str(idx_loop2) +"_"+ self.version_name) #end of while2 for line in impededPathList: if not totalConvexPathList.has_key(line): totalConvexPathList[line] = impededPathList[line] #totalConvexPathList.extend(impededPathList) totalGraph = self.createGraph(totalConvexPathList.keys()) esp_n = networkx.dijkstra_path(totalGraph, odPointsList[0], odPointsList[1]) esp = [] for i in range(len(esp_n)-1): esp.append([esp_n[i], esp_n[i+1]]) w = shapefile.Writer(shapefile.POLYLINE) w.field('nem') no_edges = 0 for line in totalConvexPathList.keys(): no_edges += 1 w.line(parts=[[ list(x) for x in line ]]) w.record('ff') w.save(self.path + "totalpath" + self.version_name + "%d" % pair[1] ) w = shapefile.Writer(shapefile.POLYLINE) w.field('nem') #calculate ESP distance ESP_dist = 0 for line in esp: segment = LineString(line) ESP_dist += segment.length ESP_dist = ESP_dist * 0.000189393939 #category a: less than 5 mi #b: 5~10 mi #c: 10 ~ for line in esp: w.line(parts=[[ list(x) for x in line ]]) w.record('ff') if ESP_dist <= 3.33: w.save(self.path + "a_ESP_" + self.version_name + "%d" % pair[1]) elif ESP_dist > 3.33: #and ESP_dist <= 10.005 w.save(self.path + "b_ESP_" + self.version_name + "%d" % pair[1]) #else: #w.save(self.path + "c_ESP_" + self.version_name + "%d" % pair[1]) #sp_len_str = str(sp_l_set)[1:-1] #s = StringIO.StringIO() #sortby = 'cumulative' #ps = pstats.Stats(pr, stream=s).sort_stats(sortby) #ps.print_stats() #print s.getvalue() # # s = StringIO.StringIO() # sortby = 'cumulative' # ps = pstats.Stats(pr2, stream=s).sort_stats(sortby) # ps.print_stats() # print s.getvalue() print "loop1: ", time_loop1 print "Spatial Filtering: ", time_spatialFiltering print "loop1 crossingDict: ", time_loop1_crossingDict print "crossingDict: ", time_crossingDict print 'convexLoop: ', time_convexLoop print "time_contain: ", time_contain2 print "impedingArcs: ", time_impedingArcs print "convexHUll: ", time_buildConvexHulls return 'convexpath %d %d %d %f %f %f' % (pair[1], no_edges, len(labeledObstaclePoly), time_convexLoop, time_crossingDict, time_buildConvexHulls)
def accept(self): # print('run') pointLayer = self.proj.mapLayersByName( self.uPointLayer.currentText())[0] #processing.getObject(str()) # check for selected features if self.uSelectedPoints.isChecked(): pointSelectFlag = True else: pointSelectFlag = False if self.uSelectedLine.isChecked(): lineSelectFlag = True else: lineSelectFlag = False ## check if need to build line if self.utabWidget.currentIndex() == 0: lineLayer = self.buildLine(pointSelectFlag) if lineLayer == 'Error': return else: lineLayer = self.proj.mapLayersByName( self.uLineLayer.currentText())[0] #processing.getObject(str()) zField = self.uZfield.currentText() pointIdField = self.uPointID.currentText() try: noData = float(self.lineEditNoData.text()) except: QMessageBox.warning(self, 'Error', 'No data value must be numeric') return if self.uBuffer.displayText() != '': buff = float(self.uBuffer.displayText()) else: buff = None # trap for coordinate system if lineLayer.crs() != pointLayer.crs(): QMessageBox.warning( self, 'Error', 'Point layer coordinate system does not match line coordinate system' ) return # trap for more than one line feature counter = 0 if lineSelectFlag: lineFeatureList = lineLayer.selectedFeatures() else: lineFeatureList = lineLayer.getFeatures() for lineFeatures in lineFeatureList: counter = counter + 1 if counter != 1: QMessageBox.warning(self, 'Error', 'More than one line feature in line layer') return if lineSelectFlag: lineFeat = lineLayer.selectedFeatures()[0] else: lineFeat = next(lineLayer.getFeatures()) lineGeom = lineFeat.geometry() if lineGeom.isMultipart: polilinha = lineGeom.asMultiPolyline()[0] #get only first else: polilinha = lineGeom.asPolyline() lineShap = LineString(polilinha) if buff: lineBoundary = lineShap.buffer(buff) if pointSelectFlag: pointFeatureList = pointLayer.selectedFeatures() else: pointFeatureList = pointLayer.getFeatures() pointList = [] for pointFeature in pointFeatureList: pointGeom = pointFeature.geometry() pointShap = Point(pointGeom.asPoint()) if buff: ## check if point is within line buffer if pointShap.within(lineBoundary): z = pointFeature[zField] ### get distance along line dist = lineShap.project(pointShap) pointId = pointFeature[pointIdField] ##store data pointList.append([dist, z, pointId]) else: z = pointFeature[zField] ### get distance along line dist = lineShap.project(pointShap) pointId = pointFeature[pointIdField] ##store data pointList.append([dist, z, pointId]) ###sort data by distance pointList = sorted(pointList, key=itemgetter(0)) ###seperate data back into individual lists zList = [] distList = [] pointIdList = [] for i in pointList: ## only keep data that is not flaged as noData if i[1] != noData: distList.append(i[0]) zList.append(i[1]) pointIdList.append(i[2]) self.values = [distList, zList, pointIdList] self.refreshPlot() self.uCopytoClip.setEnabled(True)
######################################## THESE WILL ALL GET CREATED BY US WITH FIXTURES !!!!! rx0 = Rx.objects.get(internal_name="PN13") rx1 = Rx.objects.get(internal_name="PN12") rx2 = Rx.objects.get(internal_name="PN14") #### Create spatial constraints import random if SpatialConstraint.objects.all().count() < 1: for i in range(4): pt1 = random.choice(geoms) pt2 = random.choice(geoms) from shapely.geometry import LineString line = LineString([(pt1.centroid.x, pt1.centroid.y), (pt2.centroid.x, pt2.centroid.y)]) # create line, buffer it cg1 = line.buffer(random.randint(50, 200)) sc1 = SpatialConstraint.objects.get_or_create( geom=cg1.wkt, default_rx=random.choice([rx1, rx2]), category=random.choice(["R1", "R2"]) ) ######################################## END fixtures #### Create a scenario. url = "/features/scenario/form/" print url rxs = {} for stand in stands:
def ISR_figures_demo(): import mywfc3.orient #### Pixel scale ps = {'x':0.1355, 'y':0.1211} #### dispersion offsets disp_box = {'G102': [53, 210], 'G141': [36, 168]} #### Apertures refpix_aper = {'GRISM1024':[497, 562], 'IR':[562, 562], 'IR-FIX': [512,512]} grism = 'G102' aper = 'GRISM1024' #### Demo fig = unicorn.plotting.plot_init(xs=11, aspect=1./4, square=True, left=0.01, bottom=0.01, right=0.01, top=0.01, use_tex=True, NO_GUI=True) theta_pairs = [[-20], [50], [160], [-20,50,160]] dx = 0.99/len(theta_pairs) for i in range(len(theta_pairs)): ax = fig.add_axes((0.005+i*(dx+0*0.005),0.01, dx, 0.97)) if i < 3: ax.text(0.5, 0.05, r'PA($y$) = %d' %(-theta_pairs[i][0]), transform=ax.transAxes, ha='center', va='bottom') else: ax.text(0.5, 0.05, r'PA($y$) = %d $\times$ %d $\times$ %d' %(-theta_pairs[i][0], -theta_pairs[i][1], -theta_pairs[i][2]), transform=ax.transAxes, ha='center', va='bottom') ax.scatter(refpix_aper[aper][0]*ps['x'],refpix_aper[aper][1]*ps['y'], color='purple', marker='x', s=60, label=aper) plt_range = np.array([-72, 180])*0.95 ax.set_xlim(plt_range+10); ax.set_ylim(plt_range+10) ax.set_xticklabels([]); ax.set_yticklabels([]) ax.set_xticks(ax.get_xlim()); ax.set_yticks(ax.get_ylim()) ax.plot([-1000,-1100],[-1110,-1100], color='green', alpha=0.4, linewidth=2, label=grism) if i == 0: ax.legend(scatterpoints=1, loc='upper left', fontsize=10) #theta = 70 pair = theta_pairs[i] results = [] for theta in pair: result = mywfc3.orient.overlap(theta=theta, grism=grism, have_mosaic=True, plot=False, aper='GRISM1024', f_spec=0.5, recenter_target=False) results.append(result) overlap_region = results[0][1].intersection(results[0][1]) for result in results: overlap_region = overlap_region.intersection(result[1]) for j in range(len(pair)): full_poly, sub_poly = results[j][0], results[j][1] full_patch = PolygonPatch(full_poly, fc='None', ec='black', alpha=0.9, zorder=-2) sub_patch = PolygonPatch(sub_poly, fc='black', ec='black', alpha=0.2, zorder=-2) ax.add_patch(full_patch) ax.add_patch(sub_patch) ### Death star xds, yds = threedhst.utils.xyrot(np.array([357,357])*ps['x'], np.array([55,55])*ps['y'], pair[j], x0=refpix_aper[aper][0]*ps['x'], y0=refpix_aper[aper][1]*ps['y']) ds = Point(xds[0], yds[0]).buffer(20*ps['x']) ds_patch = PolygonPatch(ds, fc='None', ec='black', alpha=0.9, zorder=-2) ax.add_patch(ds_patch) if i == 3: isect_patch = PolygonPatch(overlap_region, fc='green', ec='green', alpha=0.2, zorder=1) ax.add_patch(isect_patch) else: for xi in np.arange(-15, 180, 33): for yi in np.arange(15, 180, 30): pi = Point((xi, yi)) if pi.intersects(overlap_region): ax.scatter(xi, yi, color='green', alpha=0.8, zorder=1) trace_x = xi+np.array(disp_box[grism])*ps['x'] trace_y = np.array([yi,yi]) trace_x, trace_y = threedhst.utils.xyrot(trace_x, trace_y, pair[j], x0=xi, y0=yi) #ax.plot(trace_x, trace_y, color='green', alpha=0.4, linewidth=2, zorder=1) trace_line = LineString([(trace_x[0], trace_y[0]), (trace_x[1], trace_y[1])]) trace_in_full = trace_line.buffer(1, resolution=8) ax.add_patch(PolygonPatch(trace_in_full, fc='black', ec='None', alpha=0.15, zorder=1)) trace_in_full = trace_line.buffer(1, resolution=8).intersection(full_poly) ax.add_patch(PolygonPatch(trace_in_full, fc='green', ec='None', alpha=0.5, zorder=1)) unicorn.plotting.savefig(fig, 'orient_demo.pdf') #### Testing if False: line = LineString([(-2,0.5), (0.5, 0.5)]) full_box_x = np.array([0, 1, 1, 0, 0]) full_box_y = np.array([0, 0, 1, 1, 0]) box = Polygon(np.array([full_box_x, full_box_y]).T) line_within = line.buffer(0.02, resolution=8).intersection(box) plt.figure() ax = plt.gca() ax.add_patch(PolygonPatch(box, fc='black', ec='black', alpha=0.2, zorder=-2)) ax.add_patch(PolygonPatch(line_within, fc='green', ec='None', alpha=0.2, zorder=-1)) plt.xlim(-2,2) plt.ylim(-2,2)
def makeCrossing(in_list): crossing = defaultdict(list) lines = [] results = {} Line = LineString(in_list[0]) for obs in in_list[1]: if Line.crosses(obs): crossing[tuple(in_list[0])].append(obs) if len(crossing) == 0: return results else: containingObs = [] for obs in crossing[tuple(in_list[0])]: convexHull2 = createConvexhull(obs) chk_contain = 0 if convexHull2.contains(Point(in_list[0][0])): chk_contain = 1 containingObs.append(obs) elif convexHull2.contains(Point(in_list[0][1])): chk_contain = 1 containingObs.append(obs) if chk_contain == 0: convexHull = createConvexhull(obs, in_list[0]) splitBoundary_l(lines, convexHull) if len(containingObs) != 0: #print "SPLIT" subConvexPathList = [] vi_obs = MultiPolygon([x for x in containingObs]) containedLineCoords = list(in_list[0]) fromX = containedLineCoords[0][0] fromY = containedLineCoords[0][1] toX = containedLineCoords[1][0] toY = containedLineCoords[1][1] fxA = (fromY - toY) / (fromX - toX) fxB = fromY - (fxA * fromX) minX = vi_obs.bounds[0] maxX = vi_obs.bounds[2] split_line = LineString([(min(minX, fromX, toX), fxA * min(minX, fromX, toX) + fxB), (max(maxX, fromX, toX), fxA * max(maxX, fromX, toX) + fxB)]) for obs in containingObs: s1, s2 = splitPolygon(split_line, obs) dividedObsPoly = [] #to deal with multipolygon a = s1.intersection(obs) b = s2.intersection(obs) if a.type == "Polygon": dividedObsPoly.append(a) else: for o in a.geoms: if o.type == "Polygon": dividedObsPoly.append(o) if b.type == "Polygon": dividedObsPoly.append(b) else: for o2 in b.geoms: if o2.type == "Polygon": dividedObsPoly.append(o2) for obs2 in dividedObsPoly: for pt in in_list[0]: convexHull = createConvexhull(obs2, [pt]) splitBoundary_l(subConvexPathList, convexHull) subVertices = [] for line in subConvexPathList: subVertices.extend(line) subVertices = list(set(subVertices)) containingObsVertices = [] for obs in containingObs: containingObsVertices.extend(list(obs.exterior.coords)) subVertices = [x for x in subVertices if x in containingObsVertices] deleteList = [] for line in subConvexPathList: chk_cross = 0 for obs in containingObs: if LineString(line).crosses(obs): chk_cross = 1 if chk_cross == 1: deleteList.append(line) subConvexPathList = [x for x in subConvexPathList if x not in deleteList] pairList = [] for i in range(len(subVertices)): for j in range(i+1, len(subVertices)): pairList.append([subVertices[i], subVertices[j]]) for i in pairList: Line = LineString(i) chk_cross = 0 for obs in containingObs: if Line.crosses(obs): chk_cross = 1 elif Line.within(obs): chk_cross = 1 if chk_cross == 0: subConvexPathList.append(i) buffer_st_line = split_line.buffer(0.1) deleteList = [] for line in subConvexPathList: if buffer_st_line.contains(LineString(line)): deleteList.append(line) subConvexPathList = [x for x in subConvexPathList if x not in deleteList] lines.extend(subConvexPathList) results[tuple(in_list[0])] = lines return results
def line(x0, y0, x1, y1, width=None): l = LineString([(x0, y0), (x1, y1)]) if width is not None: l = l.buffer(width) return l
def draw_text_on_line( self, coords, text, color=(0, 0, 0), font_size=10, font_family='Tahoma', font_style=cairo.FONT_SLANT_NORMAL, font_weight=cairo.FONT_WEIGHT_NORMAL, text_halo_width=1, text_halo_color=(1, 1, 1), text_halo_line_cap=cairo.LINE_CAP_ROUND, text_halo_line_join=cairo.LINE_JOIN_ROUND, text_halo_line_dash=None, text_transform=None, ): ''' Draws text on a line. Tries to find a position with the least change in gradient and which is closest to the middle of the line. :param coords: iterable containing all coordinates as ``(lon, lat)`` :param text: text to be drawn :param color: ``(r, g, b[, a])`` :param font_size: font-size in unit (pixel/point) :param font_family: font name :param font_style: ``cairo.FONT_SLANT_NORMAL``, ``cairo.FONT_SLANT_ITALIC`` or ``cairo.FONT_SLANT_OBLIQUE`` :param font_weight: ``cairo.FONT_WEIGHT_NORMAL`` or ``cairo.FONT_WEIGHT_BOLD`` :param text_halo_width: border-width in unit (pixel/point) :param text_halo_color: ``(r, g, b[, a])`` :param text_halo_line_cap: one of :const:`cairo.LINE_CAP_*` :param text_halo_line_join: one of :const:`cairo.LINE_JOIN_*` :param text_halo_line_dash: list/tuple used by :meth:`cairo.Context.set_dash` :param text_transform: one of ``'lowercase'``, ``'uppercase'`` or ``'capitalize'`` ''' text = text.strip() if not text: return coords = map(lambda c: self.transform_coords(*c), coords) self.context.select_font_face(font_family, font_style, font_weight) self.context.set_font_size(font_size) text = utils.text_transform(text, text_transform) width, height = self.context.text_extents(text)[2:4] font_ascent, font_descent = self.context.font_extents()[0:2] self.context.new_path() #: make sure line does not intersect other conflict objects line = LineString(coords) line = self.map_area.intersection(line) line = line.difference(self.map_area.exterior.buffer(height)) line = line.difference(self.conflict_area) #: check whether line is empty or is split into several different parts if line.geom_type == 'GeometryCollection': return elif line.geom_type == 'MultiLineString': longest = None min_len = width * 1.2 for seg in line.geoms: seg_len = seg.length if seg_len > min_len: longest = seg min_len = seg_len if longest is None: return line = longest coords = tuple(line.coords) seg = utils.linestring_text_optimal_segment(coords, width) # line has either to much change in gradients or is too short if seg is None: return #: crop optimal segment of linestring start, end = seg coords = coords[start:end+1] #: make sure text is rendered from left to right if coords[-1][0] < coords[0][0]: coords = tuple(reversed(coords)) # translate linestring so text is rendered vertically in the middle line = LineString(tuple(coords)) offset = font_ascent / 2. - font_descent line = line.parallel_offset(offset, 'left', resolution=3) # make sure text is rendered centered on line start_len = (line.length - width) / 2. char_coords = None chars = utils.generate_char_geoms(self.context, text) #: draw all character paths for char in utils.iter_chars_on_line(chars, line, start_len): for geom in char.geoms: char_coords = iter(geom.exterior.coords) self.context.move_to(*char_coords.next()) for lon, lat in char_coords: self.context.line_to(lon, lat) self.context.close_path() #: only add line to reserved area if text was drawn if char_coords is not None: covered = line.buffer(height) self.conflict_union(covered) #: draw border around characters self.context.set_line_cap(cairo.LINE_CAP_ROUND) self.context.set_source_rgba(*text_halo_color) self.context.set_line_width(2 * text_halo_width) self.context.set_line_cap(text_halo_line_cap) self.context.set_line_join(text_halo_line_join) self.context.set_dash(text_halo_line_dash or tuple()) self.context.stroke_preserve() #: fill actual text self.context.set_source_rgba(*color) self.context.fill()
def road_polygon(self): if not self.direction_computed: self.compute_segment_dir() center_line_string = LineString(self.center_line) polygon = center_line_string.buffer(self.road_widths[0]) return polygon
def plot_source(coords): instance_linestring = LineString(coords[:, 0:2]) instance_patch = instance_linestring.buffer(0.5) instance_x, instance_y = instance_patch.exterior.coords.xy return instance_x, instance_y
def main(): config = load_config() data_path = config['paths']['data'] duration = 10 change_colors = [ '#1a9850', '#66bd63', '#a6d96a', '#d9ef8b', '#fee08b', '#fdae61', '#f46d43', '#d73027', '#969696' ] change_labels = [ '< -100', '-100 to -50', '-50 to -10', '-10 to 0', '0 to 10', '10 to 50', '50 to 100', ' > 100', 'No change/value' ] change_ranges = [(-1e10, -100), (-100, -50), (-50, -10), (-10, 0), (0.001, 10), (10, 50), (50, 100), (100, 1e10)] region_file_path = os.path.join(config['paths']['data'], 'network', 'rail_edges.shp') region_file = gpd.read_file(region_file_path, encoding='utf-8') fail_file = pd.read_csv( os.path.join( config['paths']['output'], 'failure_results', 'minmax_combined_scenarios', 'single_edge_failures_minmax_rail_100_percent_disrupt_multi_modal.csv' )) fail_file = fail_file[fail_file['max_tr_loss'] < 1e7] region_file = pd.merge(region_file[['edge_id', 'geometry']], fail_file[['edge_id', 'min_tr_loss', 'max_tr_loss']], how='left', on=['edge_id']).fillna(0) flow_file = pd.read_csv( os.path.join( config['paths']['output'], 'failure_results', 'minmax_combined_scenarios', 'single_edge_failures_minmax_rail_100_percent_disrupt.csv')) region_file = pd.merge( region_file, flow_file[['edge_id', 'min_econ_impact', 'max_econ_impact']], how='left', on=['edge_id']).fillna(0) fail_file = pd.merge( fail_file, flow_file[['edge_id', 'min_econ_impact', 'max_econ_impact']], how='outer', on=['edge_id']).fillna(0) del flow_file flow_file = pd.read_csv( os.path.join(config['paths']['output'], 'risk_results', 'rail_hazard_intersections_risk_weights.csv')) fail_file = pd.merge(fail_file, flow_file, how='left', on=['edge_id']).fillna(0) del flow_file fail_file['min_eael'] = duration * fail_file['risk_wt'] * fail_file[ 'min_econ_impact'] fail_file['max_eael'] = duration * fail_file['risk_wt'] * fail_file[ 'max_econ_impact'] fail_file['min_eael_multimodal'] = duration * fail_file[ 'risk_wt'] * fail_file['min_tr_loss'] fail_file['max_eael_multimodal'] = duration * fail_file[ 'risk_wt'] * fail_file['max_tr_loss'] # fail_file.to_csv('test.csv') fail_file = fail_file.groupby(['edge_id', 'climate_scenario' ])['min_eael_multimodal', 'min_eael', 'max_eael_multimodal', 'max_eael'].sum().reset_index() # fail_file.to_csv('test.csv') fail_file_min = fail_file.groupby(['edge_id' ])['min_eael_multimodal', 'min_eael'].min().reset_index() fail_file_max = fail_file.groupby(['edge_id' ])['max_eael_multimodal', 'max_eael'].max().reset_index() del fail_file region_file = pd.merge(region_file, fail_file_min, how='left', on=['edge_id']).fillna(0) region_file = pd.merge(region_file, fail_file_max, how='left', on=['edge_id']).fillna(0) del fail_file_min, fail_file_max flow_file = pd.read_csv( os.path.join(config['paths']['output'], 'flow_mapping_combined', 'weighted_flows_rail_100_percent.csv')) region_file = pd.merge(region_file, flow_file, how='left', on=['edge_id']).fillna(0) region_file = region_file[region_file['max_total_tons'] > 0] del flow_file region_file[[ 'edge_id', 'min_tr_loss', 'min_econ_impact', 'min_eael_multimodal', 'min_eael', 'max_tr_loss', 'max_econ_impact', 'max_eael_multimodal', 'max_eael' ]].to_csv(os.path.join(config['paths']['output'], 'network_stats', 'national_rail_multi_modal_risks.csv'), index=False) rail_color = '#006d2c' very_high_value = 4000000 yticks_loc = [-0.5, 0, 0.5, 1, 1.5, 2, 2.5, 3, 3.5, 4] y_ticks_labels = ['-0.5', '0', '0.5', '1', '1.5', '2', '2.5', '.', '.'] plot_sets = [ { 'file_tag': 'loss', 'no_access': [0, 1], 'legend_label': "Economic loss (million USD/day)", 'divisor': 1000000, 'columns': ['min_tr_loss', 'max_tr_loss'], 'title_cols': [ 'Rail - Multi-modal Economic impact (min)', 'Rail - Multi-modal Economic impact (max)' ] }, { 'file_tag': 'risk', 'no_access': [0, 1], 'legend_label': "EAEL million USD", 'divisor': 1000000, 'columns': ['min_eael_multimodal', 'max_eael_multimodal'], 'title_cols': ['Rail - Multi-modal EAEL (min)', 'Rail - Multi-modal EAEL (max)'] }, ] change_sets = [ { 'columns': ['tr_loss', 'econ_impact'], 'legend': 'daily losses', 'title_col': 'Percentage change in Economic losses for rail multi-modal switches' }, { 'columns': ['eael_multimodal', 'eael'], 'legend': 'EAEL', 'title_col': 'Percentage change in EAEL for rail multi-modal switches' }, ] plt_file_path = os.path.join( config['paths']['figures'], 'rail-economic-loss-ranges-100-percent-multi-modal.png') # plot_ranges(region_file[['min_tr_loss','max_tr_loss']], # 1000000, # "Percentile rank (%)", # "Economic impacts (million USD/day)", # "Rail - Range of total economic impacts due to single link failures", # rail_color,plt_file_path,very_high_value,yticks_loc,y_ticks_labels) plt_file_path = os.path.join( config['paths']['figures'], 'rail-eael-ranges-100-percent-multi-modal.png') plot_ranges(region_file[['min_eael_multimodal', 'max_eael_multimodal']], 1000000, "Percentile rank (%)", "EAEL (million USD)", "Rail - Range of EAEL due to single link failures", rail_color, plt_file_path) risk_df = region_file[['max_eael', 'max_eael_multimodal']] risk_df['zeroes'] = [0] * len(risk_df.index) risk_df['eael_multimodal'] = risk_df['max_eael'] + risk_df[ 'max_eael_multimodal'] risk_df = risk_df[(risk_df['max_eael'] > 0.5e6) | (risk_df['max_eael_multimodal'] > 0.5e6)] risk_df = risk_df.sort_values(['eael_multimodal'], ascending=True) plot_many_ranges( [ risk_df[['zeroes', 'max_eael']], risk_df[['max_eael', 'eael_multimodal']] ], 1e6, 'Percentile rank (%)', 'EAEL (million US$)', 'Rail - Max. EAEL ranges with and without multi-modal option > {:,} US$' .format(500000), ['#f03b20', '#08519c'], ['Without multi-modal options', 'With multi-modal options'], os.path.join(config['paths']['figures'], 'rail-eael-comparisons-multi-modal.png')) del risk_df for t in ['min', 'max']: for ch in change_sets: edges_vals = region_file[[ 'edge_id', 'geometry', '{}_{}'.format(t, ch['columns'][0]), '{}_{}'.format(t, ch['columns'][1]) ]] # edges_vals.to_csv('test.csv') edges_vals['change'] = edges_vals.apply( lambda x: change_to_infinity( x, '{}_{}'.format(t, ch['columns'][0]), '{}_{}'.format( t, ch['columns'][1])), axis=1) proj_lat_lon = ccrs.PlateCarree() ax = get_axes() plot_basemap(ax, data_path) scale_bar(ax, location=(0.8, 0.05)) plot_basemap_labels(ax, data_path, include_regions=True) for record in edges_vals.itertuples(): geom = record.geometry region_val = record.change if region_val: cl = [ c for c in range(len((change_ranges))) if region_val >= change_ranges[c][0] and region_val < change_ranges[c][1] ] if cl: c = cl[0] ax.add_geometries([geom], crs=proj_lat_lon, linewidth=2.0, edgecolor=change_colors[c], facecolor='none', zorder=8) # ax.add_geometries([geom.buffer(0.1)],crs=proj_lat_lon,linewidth=0,facecolor=change_colors[c],edgecolor='none',zorder=8) else: ax.add_geometries([geom], crs=proj_lat_lon, linewidth=1.5, edgecolor=change_colors[-1], facecolor='none', zorder=7) # ax.add_geometries([geom.buffer(0.1)], crs=proj_lat_lon, linewidth=0,facecolor=change_colors[-1],edgecolor='none',zorder=7) # Legend legend_handles = [] for c in range(len(change_colors)): legend_handles.append( mpatches.Patch(color=change_colors[c], label=change_labels[c])) ax.legend(handles=legend_handles, title='Percentage change in {}'.format(ch['legend']), loc=(0.5, 0.2), fancybox=True, framealpha=1.0) print(" * Plotting {}".format(ch['title_col'])) plt.title(ch['title_col'], fontsize=10) output_file = os.path.join( config['paths']['figures'], 'national-rail-multi-modal-{}-{}-change-percentage.png'.format( t, ch['legend'].replace(' ', '-'))) save_fig(output_file) plt.close() for plot_set in plot_sets: for c in range(len(plot_set['columns'])): column = plot_set['columns'][c] proj_lat_lon = ccrs.PlateCarree() ax = get_axes() plot_basemap(ax, data_path) scale_bar(ax, location=(0.8, 0.05)) plot_basemap_labels(ax, data_path, include_regions=True) weights = [ record[column] for iter_, record in region_file.iterrows() if record[column] < very_high_value ] min_weight = min(weights) max_weight = max(weights) abs_max_weight = max([abs(w) for w in weights]) width_by_range = OrderedDict() colors_by_range = {} n_steps = 8 positive_colors = [ '#f4a582', '#d6604d', '#b2182b', '#67001f', ] negative_colors = [ '#92c5de', '#4393c3', '#2166ac', '#053061', ] width_step = 0.03 mins = np.linspace(0, abs_max_weight, n_steps / 2) # mins = np.geomspace(1, abs_max_weight, n_steps/2) maxs = list(mins) maxs.append(abs_max_weight * 10) maxs = maxs[1:] # print (mins,maxs) assert len(maxs) == len(mins) # positive for i, (min_, max_) in reversed(list(enumerate(zip(mins, maxs)))): width_by_range[(min_, max_)] = (i + 2) * width_step colors_by_range[(min_, max_)] = positive_colors[i] # negative for i, (min_, max_) in enumerate(zip(mins, maxs)): width_by_range[(-max_, -min_)] = (i + 2) * width_step colors_by_range[(-max_, -min_)] = negative_colors[i] geoms_by_range = {} for value_range in width_by_range: geoms_by_range[value_range] = [] zero_value_geoms = [] for iter_, record in region_file.iterrows(): val = record[column] geom = record.geometry if val != 0 and val < very_high_value: for nmin, nmax in geoms_by_range: if nmin <= val and val < nmax: geoms_by_range[(nmin, nmax)].append(geom) else: zero_value_geoms.append(geom) # plot for range_, width in width_by_range.items(): ax.add_geometries( [geom.buffer(width) for geom in geoms_by_range[range_]], crs=proj_lat_lon, edgecolor='none', facecolor=colors_by_range[range_], zorder=2) width_min = min( [width for range_, width in width_by_range.items()]) ax.add_geometries( [geom.buffer(width_min) for geom in zero_value_geoms], crs=proj_lat_lon, edgecolor='none', facecolor='#969696', zorder=1) x_l = -62.4 x_r = x_l + 0.4 base_y = -42.1 y_step = 0.8 y_text_nudge = 0.2 x_text_nudge = 0.2 ax.text(x_l - x_text_nudge, base_y + y_step - y_text_nudge, plot_set['legend_label'], horizontalalignment='left', transform=proj_lat_lon, size=8) divisor = plot_set['divisor'] i = 0 for (nmin, nmax), width in width_by_range.items(): if not geoms_by_range[(nmin, nmax)]: continue y = base_y - (i * y_step) i = i + 1 line = LineString([(x_l, y), (x_r, y)]) ax.add_geometries([line.buffer(width)], crs=proj_lat_lon, linewidth=0, edgecolor=colors_by_range[(nmin, nmax)], facecolor=colors_by_range[(nmin, nmax)], zorder=2) if nmin == max_weight: label = '>{:.2f}'.format(max_weight / divisor) elif nmax == -abs_max_weight: label = '<{:.2f}'.format(-abs_max_weight / divisor) else: label = '{:.2f} to {:.2f}'.format(nmin / divisor, nmax / divisor) ax.text(x_r + x_text_nudge, y - y_text_nudge, label, horizontalalignment='left', transform=proj_lat_lon, size=8) styles = OrderedDict([ ('1', Style(color='#b2182b', zindex=9, label='Economic loss effect')), # green ('2', Style(color='#2166ac', zindex=9, label='Economic gain effect')), ('3', Style(color='#969696', zindex=9, label='No hazard exposure/effect')) ]) plt.title(plot_set['title_cols'][c], fontsize=14) legend_from_style_spec(ax, styles, loc='lower left') print('* Plotting Rail {}'.format(plot_set['title_cols'][c])) output_file = os.path.join( config['paths']['figures'], 'rail_failure-map-{}-{}-multi-modal-options.png'.format( plot_set['file_tag'], column)) save_fig(output_file) plt.close() print(" >", output_file)
def _check(self, aerodrome_type, runway_kind_detail): import dsl from tilequeue.tile import coord_to_bounds from shapely.geometry import LineString from shapely.geometry import CAP_STYLE from ModestMaps.Core import Coordinate z, x, y = (16, 0, 0) bounds = coord_to_bounds(Coordinate(zoom=z, column=x, row=y)) # runway line that runs from a quarter to three quarters of the # tile diagonal. this is so that we can buffer it without it # going outside the tile boundary. runway_line = LineString([ [ bounds[0] + 0.25 * (bounds[2] - bounds[0]), bounds[1] + 0.25 * (bounds[3] - bounds[1]) ], [ bounds[0] + 0.75 * (bounds[2] - bounds[0]), bounds[1] + 0.75 * (bounds[3] - bounds[1]) ], ]) # runway polygon which has the runway line as a centreline. runway_poly = runway_line.buffer( 0.1 * (bounds[2] - bounds[0]), # width 1/10th of a tile cap_style=CAP_STYLE.flat, ) self.generate_fixtures( dsl.way( 1, dsl.tile_box(z, x, y), { 'aeroway': 'aerodrome', 'aerodrome:type': aerodrome_type, 'name': 'Fake Aerodrome', 'source': 'openstreetmap.org', }), # runway line dsl.way(2, runway_line, { 'aeroway': 'runway', 'source': 'openstreetmap.org', }), # runway polygon dsl.way(3, runway_poly, { 'area:aeroway': 'runway', 'source': 'openstreetmap.org', })) # runway line ends up in roads layer self.assert_has_feature( z, x, y, 'roads', { 'id': 2, 'kind': 'aeroway', 'kind_detail': 'runway', 'aerodrome_kind_detail': runway_kind_detail, }) # runway polygon is in landuse self.assert_has_feature(z, x, y, 'landuse', { 'id': 3, 'kind': 'runway', 'kind_detail': runway_kind_detail, })
def get_buffer_route(normal_route): route = LineString(normal_route["features"][0]["geometry"]["coordinates"]) buffer_route = route.buffer(0.005) return buffer_route
def valley_bottom(self): """ Run the VBET algorithm :return: saves a valley bottom shapefile """ for i in self.network.index: seg = self.network.loc[i] da = seg['Drain_Area'] geom = seg['geometry'] print 'segment ', i + 1, ' of ', len(self.network.index) ept1 = (geom.boundary[0].xy[0][0], geom.boundary[0].xy[1][0]) ept2 = (geom.boundary[1].xy[0][0], geom.boundary[1].xy[1][0]) line = LineString([ept1, ept2]) if da >= self.lg_da: buf = line.buffer(self.lg_buf, cap_style=1) elif da < self.lg_da and da >= self.med_da: buf = line.buffer(self.med_buf, cap_style=1) else: buf = line.buffer(self.sm_buf, cap_style=1) bufds = gpd.GeoSeries(buf) coords = self.getFeatures(bufds) with rasterio.open(self.dem) as src: out_image, out_transform = rasterio.mask.mask(src, coords, crop=True) out_meta = src.meta.copy() out_meta.update({ 'driver': 'Gtiff', 'height': out_image.shape[1], 'width': out_image.shape[2], 'transform': out_transform }) with rasterio.open(self.scratch + '/dem_sub.tif', 'w', **out_meta) as dest: dest.write(out_image) dem = self.scratch + "/dem_sub.tif" demsrc = rasterio.open(dem) demarray = demsrc.read()[0, :, :] ndval = demsrc.nodata slope = self.slope(dem) if da >= self.lg_da: slope_sub = self.reclassify(slope, ndval, self.lg_slope) elif da < self.lg_da and da >= self.med_da: slope_sub = self.reclassify(slope, ndval, self.med_slope) else: slope_sub = self.reclassify(slope, ndval, self.sm_slope) # only detrend if depth is being used if self.med_depth is not None: detr = self.detrend(dem, geom) # might want to change this offset if da >= self.lg_da: depth = self.reclassify(detr, ndval, self.lg_depth) elif da < self.lg_da and da >= self.med_da: depth = self.reclassify(detr, ndval, self.med_depth) else: depth = self.reclassify(detr, ndval, self.sm_depth) overlap = self.raster_overlap(slope_sub, depth, ndval) filled = self.fill_raster_holes(overlap, 30000, ndval) a = self.raster_to_shp(filled, dem) self.network.loc[i, 'fp_area'] = a else: filled = self.fill_raster_holes(slope_sub, 15000, ndval) a = self.raster_to_shp(filled, dem) self.network.loc[i, 'fp_area'] = a self.network.to_file(self.streams) # merge all polygons in folder and dissolve vb = gpd.GeoSeries(cascaded_union(self.polygons)) # vb.crs = self.crs_out vb.to_file(self.scratch + "/tempvb.shp") # get rid of small unattached polygons vb1 = gpd.read_file(self.scratch + "/tempvb.shp") vbm2s = vb1.explode() sub = vbm2s['geometry'].area >= 0.25 * vbm2s['geometry'].area.max() vbcut = vbm2s[sub] vbcut.to_file(self.scratch + "/tempvb.shp") # simplify and smooth polygon vbc = gpd.read_file(self.scratch + "/tempvb.shp") vbc = vbc.simplify( 3, preserve_topology=True) # make number a function of dem resolution coords = list(vbc.geometry.exterior[0].coords) new_coords = self.chaikins_corner_cutting(coords) poly = Polygon(new_coords) vbf = gpd.GeoDataFrame(index=[0], crs=self.crs_out, geometry=[poly]) vbf.to_file(self.out) return
from matplotlib import pyplot from shapely.geometry import Point, LineString from descartes import PolygonPatch from shapely.ops import cascaded_union #from figures import SIZE, BLUE, GRAY fig = pyplot.figure(1, figsize=(10, 10), dpi=90) polygons = [] a = Point(1, 1).buffer(1.5) b = Point(2, 1).buffer(1.5) # line to polygon! line = LineString([(0, 0), (0, 1), (0, 2), (1, 2), (3, 3)]) polygons.append(line.buffer(0.1)) ps = cascaded_union(polygons) print(ps) # 1 ax = fig.add_subplot(121) patch1 = PolygonPatch(ps, fc=(0.10, 0.10, 0.10), ec=(0.10, 0.10, 0.10), alpha=0.2, zorder=1) ax.add_patch(patch1) patch2 = PolygonPatch(b, fc=(0.10, 0.10, 0.10), ec=(0.10, 0.10, 0.10), alpha=0.2, zorder=1) ax.add_patch(patch2)
def combine_grid_x_and_grid_y(row): return str(row['grid_x']) + str(row['grid_y']) def remove_consecutive_duplicate_locations_from_gps_dataframe(gps_df): if 'grid_x' in gps_df.columns and 'grid_y' in gps_df.columns: gps_df['grid_xy'] = gps_df.apply(combine_grid_x_and_grid_y, axis=1) mask = gps_df['grid_xy'].shift() != gps_df['grid_xy'] gps_df_new = gps_df.loc[mask] return gps_df_new else: print("Columns grid_x or grid_y are not in the dataframe!") exit(0) # dir = "/Volumes/Seagate_Rui/dimensionality/data/taxi/test/" # for file in os.listdir(dir): # df = pd.read_csv(dir+file) # # df_sorted = df.sort_values(['DutyCycle']) # buffer_area = calc_buffer_area(df.copy(deep=True), 'Taxi') # print("buffer area is", buffer_area) df = pd.read_csv("/Users/ruizhang/Documents/GitHub/Leetcode/test.csv") grid_path = df.loc[:, ['grid_x','grid_y']].values.tolist() path = LineString(grid_path) print(path) print("path is valid?", path.is_valid) buffer_path = path.buffer(2, cap_style=1) # df_sorted = df.sort_values(['DutyCycle']) # buffer_area = calc_buffer_area(df.copy(deep=True), 'Taxi') # print("buffer area is", buffer_area)
def plot_creature(event): if len(source.selected.indices) > 0: creature_index = source.selected.indices[0] creature = plotData.iloc[creature_index, :] coords = np.array(ast.literal_eval(creature['Coordinates'])) L_string.text = '{}'.format(creature['L-string']) area_label.text = 'Area: {:.2f}'.format(creature['Area']) length_label.text = 'Length of L-string: {}'.format( len(creature['L-string'])) gram_frame_1 = pd.DataFrame.from_dict( { '2-gram': creature['2-gram'], '3-gram': creature['3-gram'], '4-gram': creature['4-gram'], '5-gram': creature['5-gram'], }, orient='index').T counts = [ pd.value_counts( gram_frame_1[i]).reset_index().astype(str).apply( ' '.join, 1) for i in gram_frame_1 ] out = pd.concat(counts, 1).fillna('') out.columns = gram_frame_1.columns grams.text = str(tabulate(out, headers='keys')) patch_x, patch_y = plot_source(coords) # x_points = [list(patch_x)] # y_points = [list(patch_y)] rules = ast.literal_eval(creature['Rules']) rules = rules['X'] rules = rules['options'] rule1_c = mapper(rules[0], creature['Angle']) rule2_c = mapper(rules[1], creature['Angle']) rule_text.text = rules[0] + '\n' + rules[1] rule1_morphology = LineString(rule1_c[:, 0:2]) rule1_patch = rule1_morphology.buffer(0.5) rpatch_x, rpatch_y = rule1_patch.exterior.coords.xy rule2_morphology = LineString(rule2_c[:, 0:2]) rule2_patch = rule2_morphology.buffer(0.5) r2patch_x, r2patch_y = rule2_patch.exterior.coords.xy inside_x = [] inside_y = [] # for i, _ in enumerate(creature_patch.interiors): # x_in, y_in = creature_patch.interiors[i].coords.xy # # x_points.append(list(x_in)) # # y_points.append(list(y_in)) # inside_x.append(x_in) # inside_y.append(x_in) # x_points = [[x_points]] # y_points = [[y_points]] # source2.data = dict(x=x_points, y=y_points) source1.data = dict(x=coords[:, 0], y=coords[:, 1]) source2.data = dict(x=patch_x, y=patch_y) source3.data = dict(x=inside_x, y=inside_y) rule1.data = dict(x=rpatch_x, y=rpatch_y) rule2.data = dict(x=r2patch_x, y=r2patch_y) p3.x_range = (min(coords), max(coords)) p3.match_aspect = True else: source1.data = dict(x=[0, 0], y=[0, 0]) source2.data = dict(x=[0, 0], y=[0, 0]) source3.data = dict(x=[0, 0], y=[0, 0]) rule1.data = dict(x=[0, 0], y=[0, 0]) rule2.data = dict(x=[0, 0], y=[0, 0]) L_string.text = 'Select creature' area_label.text = 'Select creature' length_label.text = 'Select creature' rule_text.text = 'Select creature'
def main(): config = load_config() inland_edge_file = os.path.join( config['paths']['data'], 'Results', 'Flow_shapefiles', 'weighted_edges_flows_national_inland.shp') color = '#0689d7' color_by_type = {'Inland Line': color} crop_cols = [ 'max_rice', 'max_cash', 'max_cass', 'max_teas', 'max_maiz', 'max_rubb', 'max_swpo', 'max_acof', 'max_rcof', 'max_pepp' ] ind_cols = [ 'max_sugar', 'max_wood', 'max_steel', 'max_constr', 'max_cement', 'max_fertil', 'max_coal', 'max_petrol', 'max_manufa', 'max_fisher', 'max_meat', 'max_tons' ] columns = crop_cols + ind_cols column_label_divisors = {c: 1 for c in columns} legend_label = "AADF (tons/day)" title_cols = [ 'Rice', 'Cashew', 'Cassava', 'Teas', 'Maize', 'Rubber', 'Sweet Potatoes', 'Coffee Arabica', 'Coffee Robusta', 'Pepper', 'Sugar', 'Wood', 'Steel', 'Construction materials', 'Cement', 'Fertilizer', 'Coal', 'Petroleum', 'Manufacturing', 'Fishery', 'Meat', 'Total tonnage' ] remove_routes_ids = [ ('watern_149', 'watern_429'), ('watern_429', 'watern_520'), ('watern_700', 'watern_520'), ('watern_210', 'watern_700'), ('watern_209', 'watern_210'), ('watern_1057', 'watern_1050'), ('watern_1050', 'watern_1051'), ('watern_1051', 'watern_183'), ('watern_183', 'watern_354'), ('watern_176', 'watern_354'), ] for c in range(len(columns)): ax = get_axes() plot_basemap(ax, config['paths']['data']) scale_bar(ax, location=(0.8, 0.05)) plot_basemap_labels(ax, config['paths']['data']) proj_lat_lon = ccrs.PlateCarree() column = columns[c] weights = [ record.attributes[column] for record in shpreader.Reader(inland_edge_file).records() ] max_weight = max(weights) width_by_range = generate_weight_bins(weights) geoms_by_range = {} for value_range in width_by_range: geoms_by_range[value_range] = [] for record in shpreader.Reader(inland_edge_file).records(): val = record.attributes[column] geom = record.geometry edge_id = (record.attributes['from_node'], record.attributes['to_node']) if val > 0 and (edge_id not in remove_routes_ids ): # only add edges that carry this commodity for nmin, nmax in geoms_by_range: if nmin <= val and val < nmax: geoms_by_range[(nmin, nmax)].append(geom) # plot for range_, width in width_by_range.items(): ax.add_geometries( [geom.buffer(width) for geom in geoms_by_range[range_]], crs=proj_lat_lon, edgecolor='none', facecolor=color, zorder=2) x_l = 102.3 x_r = x_l + 0.4 base_y = 14 y_step = 0.4 y_text_nudge = 0.1 x_text_nudge = 0.1 ax.text(x_l, base_y + y_step - y_text_nudge, legend_label, horizontalalignment='left', transform=proj_lat_lon, size=10) divisor = column_label_divisors[column] for (i, ((nmin, nmax), width)) in enumerate(width_by_range.items()): y = base_y - (i * y_step) line = LineString([(x_l, y), (x_r, y)]) ax.add_geometries([line.buffer(width)], crs=proj_lat_lon, linewidth=0, edgecolor=color, facecolor=color, zorder=2) if nmin == max_weight: label = '>{:.2f}'.format(max_weight / divisor) else: label = '{:.2f}-{:.2f}'.format(nmin / divisor, nmax / divisor) ax.text(x_r + x_text_nudge, y - y_text_nudge, label, horizontalalignment='left', transform=proj_lat_lon, size=10) plt.title(title_cols[c], fontsize=14) output_file = os.path.join(config['paths']['figures'], 'inland_flow-map-{}.png'.format(column)) save_fig(output_file) plt.close()
class Road(WebotsObject): """Road class representing a Webots Road.""" noIntersectionRoadLines = False speedLimit = None roadNameIndex = 1 pedestrianCrossingNameIndex = 1 nameList = [] roads = [] # Road instances container crossroads = {} # Crossroad instances container allRoadWayPoints = [] # Stores a copy of all the exported waypoints def __init__(self): """Initialize the road.""" self.osmid = '' self.id = '' self.refs = 0 self.tags = {} self.crossings = [] self.originalPath = None self.finalPath = None self.shape = None self.startingAngle = None self.endingAngle = None self.startJunction = None self.endJunction = None def parse_tags(self): """Extract road data from the OSM tags and the Settings. Returns the validity of this road.""" self.name = "" self.streetName = "" self.lanes = 2 self.forwardLanes = 1 self.backwardLanes = 1 self.turnLanesForward = None self.turnLanesBackward = None self.noBorder = False self.layer = 0.0 self.maxSpeed = -1.0 self.type = self.tags['highway'] settingsSection = 'road_' + self.type if Settings.has_option('road', 'filter'): ids = [str(x) for x in Settings.get('road', 'filter').split(', ')] if self.osmid not in ids: return False else: if settingsSection not in Settings.sections(): settingsSection = 'road' if settingsSection not in Settings.sections(): return False if Settings.has_option(settingsSection, 'ignore') and Settings.get( settingsSection, 'ignore') == 'TRUE': return False if 'name' in self.tags: self.streetName = self.tags['name'] if 'lanes' in self.tags: self.lanes = int(self.tags['lanes']) self.forwardLanes = self.lanes - self.backwardLanes assert self.lanes > 0 elif Settings.has_option(settingsSection, 'laneNumber'): self.lanes = Settings.getint(settingsSection, 'laneNumber') if 'layer' in self.tags: self.layer = float(self.tags['layer']) try: if 'lanes:forward' in self.tags: forwardLanes = int(self.tags['lanes:forward']) assert forwardLanes > 0 assert self.lanes - forwardLanes >= 0 self.forwardLanes = forwardLanes self.backwardLanes = self.lanes - self.forwardLanes except (ValueError, AssertionError): print('Invalid "lanes:forward" tag for "%s"' % self.name) try: if 'lanes:backward' in self.tags: backwardLanes = int(self.tags['lanes:backward']) lanes = self.lanes forwardLanes = self.forwardLanes # note: "lanes" is overriden if "lanes:backward" and "lanes:forward" are both defined. if 'lanes:forward' in self.tags: lanes = forwardLanes + backwardLanes else: forwardLanes = lanes - backwardLanes assert backwardLanes > 0 assert lanes - backwardLanes >= 0 self.backwardLanes = backwardLanes self.forwardLanes = forwardLanes self.lanes = lanes except (ValueError, AssertionError): print('Invalid "lanes:backward" tag for "%s"' % self.name) # Note: "turn:lanes" seems to have no influence on the lanes in JOSM. self.insideARoundAbout = 'junction' in self.tags and self.tags[ 'junction'] == 'roundabout' self.oneway = 'oneway' in self.tags and self.tags['oneway'] == 'yes' if self.oneway or self.insideARoundAbout: self.backwardLanes = 0 self.forwardLanes = self.lanes if self.backwardLanes == 0 and self.forwardLanes > 0: self.oneway = True if 'turn:lanes:forward' in self.tags: self.turnLanesForward = self.tags['turn:lanes:forward'] if len(self.turnLanesForward.split('|')) != self.forwardLanes: print('Invalid "turn:lanes:forward" tag for "%s"' % self.name) self.turnLanesForward = None if 'turn:lanes:backward' in self.tags: self.turnLanesBackward = self.tags['turn:lanes:backward'] if len(self.turnLanesBackward.split('|')) != self.backwardLanes: print('Invalid "turn:lanes:backward" tag for "%s"' % self.name) self.turnLanesBackward = None self.width = None if 'width' in self.tags: # ref. http://wiki.openstreetmap.org/wiki/Key:width # Note: only the documented "width" values are supported, # even if a lot of other values could be present: # https://taginfo.openstreetmap.org/keys/?key=width#values m = re.match(r'(\d+)\s*\'\s*(\d*)\s*"?', self.tags['width']) if m: # Attempt to extract these cases: "3'" or "4'12\"" self.width = int(m.group(1)) * 0.3048 # foot to meters if m.group(2): self.width += int(m.group(2)) * 0.0254 # inch to meters else: m = re.match(r'(\d*\.\d+|\d+)\s*(.*)', self.tags['width']) if m: # Attempt to extract these cases: "3", "3.3", "3 mi" or "3 km" self.width = float(m.group(1)) if m.group(2): if m.group(2) == 'mi': self.width *= 1609.34 # miles to meters elif m.group(2) == 'km': self.width *= 1000.0 # km to meters else: self.width = None else: self.width = None if self.width is None: # Failed to extract width. print('Warning: Invalid "width" tag for "%s" OSMID.: "%s"' % (self.osmid, self.tags['width'])) if self.width is None: laneWidth = 3.5 if Settings.has_option(settingsSection, 'laneWidth'): laneWidth = Settings.getfloat(settingsSection, 'laneWidth') self.width = laneWidth * self.lanes if Settings.has_option(settingsSection, 'border') and Settings.get( settingsSection, 'border') == 'FALSE': self.noBorder = True return True def is_similar(self, other): """Determine if a road has the same graphical shape than another in order to be merged.""" if (self.oneway and not other.oneway) or (not self.oneway and other.oneway): return False # reject if the oneway attribute is not matching if self.oneway: # both roads are oneway return (self.lanes == other.lanes and self.forwardLanes == other.forwardLanes and self.backwardLanes == other.backwardLanes and abs(self.width - other.width) <= 0.01) else: # both roads are bidirectional return (self.lanes == other.lanes and self.forwardLanes == other.backwardLanes and self.backwardLanes == other.forwardLanes and abs(self.width - other.width) <= 0.01) def is_inside_a_city(self): """Return True if the road is inside a city.""" # The best found criteria to determine if a road is inside a city or not # is the fact that this road or its connecting roads are tagged as residential. # cf. https://help.openstreetmap.org/questions/56876/how-to-determine-if-a-road-is-outside-or-inside-place if 'residential' in self.type.lower(): return True connectedJunctions = [] if self.startJunction is not None: connectedJunctions.append(self.startJunction) if self.endJunction is not None: connectedJunctions.append(self.endJunction) for crossroad in connectedJunctions: for connectedRoad in crossroad.roads: if self != connectedRoad and 'residential' in connectedRoad.type.lower( ): return True return False def process_path(self): """Convert the OSM way to a shapely geometry.""" points = [] for ref in self.refs: if ref in OSMCoord.coordDictionnary: p = Point(OSMCoord.coordDictionnary[ref].x, OSMCoord.coordDictionnary[ref].z) points.append(p) if len(self.refs) >= 2: self.originalPath = LineString(points) self.finalPath = self.originalPath else: self.originalPath = None self.finalPath = None def smooth_sharp_angles(self): """Smooth sharp angles from self.""" if self.originalPath is None or not isinstance( self.originalPath, LineString) or len(self.originalPath.coords) < 3: return angleThreshold = 1.3 # angle threshlod in radian from which an angle is considered as sharp distanceThreshold = 5.0 # distance threshlod in meters from which a distance can be considered as sharp newPointsDistance = 3.0 # distance in meters where to add new points path = [] path.append( [self.originalPath.coords[0][0], self.originalPath.coords[0][1]]) for i in range(len(self.originalPath.coords) - 2): p1 = Vector2D(self.originalPath.coords[i][0], self.originalPath.coords[i][1]) p2 = Vector2D(self.originalPath.coords[i + 1][0], self.originalPath.coords[i + 1][1]) p3 = Vector2D(self.originalPath.coords[i + 2][0], self.originalPath.coords[i + 2][1]) v1to2 = p2 - p1 v2to3 = p3 - p2 angle = v1to2.angle(v2to3) if abs(angle) > angleThreshold and v1to2.norm( ) > distanceThreshold and v2to3.norm() > distanceThreshold: # sharp angle detected in p2 p2before = p2 + v1to2.normalize() * (-newPointsDistance) p2after = p2 + v2to3.normalize() * newPointsDistance p2new = ((p2before + p2after) * 0.5 + p2) * 0.5 path.append([p2before.x, p2before.y]) path.append([p2new.x, p2new.y]) path.append([p2after.x, p2after.y]) else: path.append([p2.x, p2.y]) path.append( [self.originalPath.coords[-1][0], self.originalPath.coords[-1][1]]) self.originalPath = LineString(path) self.finalPath = self.originalPath def process_shape(self): """Create the shapely polygon from the shapely path.""" if self.originalPath is not None: self.shape = self.originalPath.buffer(0.5 * self.width, cap_style=2, join_style=2) def split_at(self, node): """Split a road in 2 at some node.""" assert node in self.refs a = copy.deepcopy(self) cut = next((i for i, v in enumerate(a.refs) if v == node)) assert cut != 0 and cut != len(self.refs) - 1 a.refs = a.refs[:(cut + 1)] b = copy.deepcopy(self) b.refs = b.refs[cut:] return [a, b] @staticmethod def add_to_list(osmid, tags, refs): """Add a new road to the list of roads.""" settingsSection = Settings.get_section('road', tags['highway']) if settingsSection is None: return road = Road() road.osmid = osmid road.id = str(osmid) road.refs = refs road.tags = tags if road.parse_tags(): # Only add the road if valid. Road.roads.append(road) for ref in road.refs: node = OSMNode.nodeDictionnary[ref] if 'highway' in node.tags and node.tags['highway'] == 'crossing': road.crossings.append(node) @classmethod def initialize_speed_limit(cls, country): """Create the SpeedLimit instance.""" cls.speedLimit = SpeedLimit(country) @classmethod def export(cls, f): """Export all the roads from the roads list.""" # Export crossroads sortedCrossroads = sorted(cls.crossroads.values(), key=lambda x: x.id) for crossroad in sortedCrossroads: translation = Vector2D( OSMCoord.coordDictionnary[crossroad.nodeRef].x, OSMCoord.coordDictionnary[crossroad.nodeRef].z) f.write('Crossroad {\n') f.write(' translation %f %f %f\n' % (translation.x, vOffset, translation.y)) if crossroad.name is not None: name = crossroad.name i = 1 while name in Crossroad.nameList: name = crossroad.name + '(%d)' % i i += 1 Crossroad.nameList.append(name) f.write(' name "%s"\n' % name) else: f.write(' name "crossroad(%d)"\n' % Crossroad.nameIndex) Crossroad.nameIndex += 1 f.write(' id "%s"\n' % crossroad.id) f.write(' shape [\n') if crossroad.shape is not None: coords = convert_polygon_to_vector2d_list(crossroad.shape) for coord in coords: height = 0 if WebotsObject.enable3D: osmCoord = cls._convert_webots_to_osm_coord( [coord.x, coord.y]) height += WebotsObject.elevation.interpolate_height( osmCoord[0], osmCoord[1]) f.write(' %f %f %f\n' % (coord.x - translation.x, height, coord.y - translation.y)) f.write(' ]\n') f.write(' connectedRoadIDs [\n') sortedRoads = sorted(crossroad.roads, key=lambda x: x.id) for road in sortedRoads: f.write(' "%s"\n' % (road.id)) f.write(' ]\n') f.write('}\n') # Export roads for road in cls.roads: if not isinstance(road.finalPath, LineString): continue coords = road.finalPath.coords f.write('Road {\n') f.write(' translation %f %f %f\n' % (coords[0][0], vOffset + road.layer * WebotsObject.layerHeight, coords[0][1])) if road.streetName: name = road.streetName i = 1 while name in Road.nameList: name = road.streetName + '(%d)' % i i += 1 Road.nameList.append(name) f.write(' name "%s"\n' % name.replace('"', '')) else: f.write(' name "road(%d)"\n' % Road.roadNameIndex) Road.roadNameIndex += 1 f.write(' id "%s"\n' % road.id) if road.startJunction is not None: f.write(' startJunction "%s"\n' % road.startJunction.id) if road.endJunction is not None: f.write(' endJunction "%s"\n' % road.endJunction.id) f.write(' splineSubdivision 0\n') f.write(' numberOfLanes %d\n' % road.lanes) f.write(' numberOfForwardLanes %d\n' % road.forwardLanes) if road.maxSpeed > 0.0: f.write(' speedLimit %f\n' % road.maxSpeed) f.write(' width %f\n' % road.width) if road.noBorder: f.write(' leftBorder FALSE\n') f.write(' rightBorder FALSE\n') if road.startingAngle: if road.startingAngle > math.pi: road.startingAngle -= 2 * math.pi elif road.startingAngle < -math.pi: road.startingAngle += 2 * math.pi f.write(' startingAngle %f\n' % road.startingAngle) if road.endingAngle: if road.endingAngle > math.pi: road.endingAngle -= 2 * math.pi elif road.endingAngle < -math.pi: road.endingAngle += 2 * math.pi f.write(' endingAngle %f\n' % road.endingAngle) f.write(' lines [\n') for i in range(road.lanes - 1): f.write(' RoadLine {\n') f.write(' type "%s"\n' % ('continuous' if (i == road.forwardLanes - 1) else 'dashed')) f.write(' }\n') f.write(' ]\n') if road.turnLanesForward: f.write(' turnLanesForward "%s"\n' % road.turnLanesForward) if road.turnLanesBackward: f.write(' turnLanesBackward "%s"\n' % road.turnLanesBackward) if not Road.noIntersectionRoadLines and not road.insideARoundAbout: if road.startJunction is not None and len( road.startJunction.roads) > 2: f.write(' startLine [\n') for l in range(road.forwardLanes): f.write(' "textures/road_line_dashed.png"\n') for l in range(road.backwardLanes): f.write(' "textures/road_line_triangle.png"\n') f.write(' ]\n') if road.endJunction is not None and len( road.endJunction.roads) > 2: f.write(' endLine [\n') for l in range(road.forwardLanes): f.write(' "textures/road_line_triangle.png"\n') for l in range(road.backwardLanes): f.write(' "textures/road_line_dashed.png"\n') f.write(' ]\n') f.write(' wayPoints [\n') for coord in coords: height = 0 if WebotsObject.enable3D: osmCoord = cls._convert_webots_to_osm_coord(coord) height += WebotsObject.elevation.interpolate_height( osmCoord[0], osmCoord[1]) f.write( ' %f %f %f\n' % (coord[0] - coords[0][0], height, coord[1] - coords[0][1])) Road.allRoadWayPoints.append([coord[0], coord[1]]) f.write(' ]\n') f.write('}\n') # Export pedestrian crossings for crossingNode in road.crossings: for i in range(len(road.refs)): ref = road.refs[i] translation = Vector2D( OSMCoord.coordDictionnary[crossingNode.OSMID].x, OSMCoord.coordDictionnary[crossingNode.OSMID].z) if (translation.x == OSMCoord.coordDictionnary[ref].x and translation.y == OSMCoord.coordDictionnary[ref].z): if i == len(road.refs) - 1: otherRef = road.refs[i - 1] else: otherRef = road.refs[i + 1] alpha = math.atan( (OSMCoord.coordDictionnary[otherRef].x - OSMCoord.coordDictionnary[ref].x) / (OSMCoord.coordDictionnary[otherRef].z - OSMCoord.coordDictionnary[ref].z)) width = road.width for crossroad in cls.crossroads.values(): # If the pedestrian crossing is on crossroad, it is moved. if (translation.x == OSMCoord.coordDictionnary[ crossroad.nodeRef].x and translation.y == OSMCoord. coordDictionnary[crossroad.nodeRef].z): # Crossing must be parallel to this vector. vector = Vector2D( OSMCoord.coordDictionnary[otherRef].x - OSMCoord.coordDictionnary[ref].x, OSMCoord.coordDictionnary[otherRef].z - OSMCoord.coordDictionnary[ref].z) normVector = math.sqrt(vector.x**2 + vector.y**2) distanceFromCenter = 6.0 # distance to add to the translation in the good direction if crossroad.shape is not None: bounds = crossroad.shape.bounds # center of the crossroad centre = Vector2D( (bounds[2] + bounds[0]) / 2, (bounds[3] + bounds[1]) / 2) # difference between crossroad point and center difference = Vector2D( centre.x - OSMCoord.coordDictionnary[ crossroad.nodeRef].x, centre.y - OSMCoord.coordDictionnary[ crossroad.nodeRef].z) beta = math.atan(difference.x / difference.y) normDifference = math.sqrt( difference.x**2 + difference.y**2) # radius of the crossroad radius = math.sqrt(( (bounds[2] - bounds[0]) / 2)**2 + ( (bounds[3] - bounds[1]) / 2)**2) # We need to add a correction distance to crossing translation because the center of the # crossroad is not the same point as the crossroad point. corrector = normDifference * abs( math.cos(beta - alpha)) if normVector > math.sqrt( (centre.x - OSMCoord. coordDictionnary[otherRef].x)**2 + (centre.y - OSMCoord. coordDictionnary[otherRef].z)**2): corrector = -corrector distanceFromCenter = radius - corrector translation = Vector2D( OSMCoord.coordDictionnary[ref].x + (distanceFromCenter / normVector * vector.x), OSMCoord.coordDictionnary[ref].z + (distanceFromCenter / normVector * vector.y)) break f.write('PedestrianCrossing {\n') f.write(' translation %f %f %f\n' % (translation.x, -0.07, translation.y)) f.write(' rotation 0 1 0 %f\n' % (alpha)) f.write(' name "pedestrian crossing(%d)"\n' % (Road.pedestrianCrossingNameIndex)) Road.pedestrianCrossingNameIndex += 1 f.write(' size %f %f\n' % (width, width * 0.4)) f.write('}\n') break @classmethod def process(cls): """Process the roads before exportation.""" # split the roads at crossroads while True: modified = False for roadA in Road.roads: for refA in roadA.refs: for roadB in Road.roads: for refB in roadB.refs: if refA == refB and not roadA == roadB: ref = refA for r in [roadA, roadB]: if r.refs[0] != ref and r.refs[-1] != ref: [r1, r2] = r.split_at(ref) Road.roads.remove(r) r1.id = Road.generate_unique_road_id( r.osmid) Road.roads.append(r1) r2.id = Road.generate_unique_road_id( r.osmid) Road.roads.append(r2) modified = True if modified: break if modified: break if modified: break if modified: break if not modified: break # create crossroads, and link road <-> crossroad. cls.crossroads = {} for roadA in Road.roads: for refA in roadA.refs: for roadB in Road.roads: for refB in roadB.refs: if refA == refB and not roadA == roadB: crossroad = None if refA in cls.crossroads: crossroad = cls.crossroads[refA] else: crossroad = Crossroad(refA) cls.crossroads[refA] = crossroad crossroad.add_road(roadA) crossroad.add_road(roadB) for crossroad in cls.crossroads.values(): for road in crossroad.roads: if road.refs[0] == crossroad.nodeRef: road.startJunction = crossroad if road.refs[-1] == crossroad.nodeRef: road.endJunction = crossroad # The speed limit can only be computed at this point, because the final network need to be known # to evaluate if the road is inside or outside a city. if cls.speedLimit is not None: for road in Road.roads: road.maxSpeed = cls.speedLimit.compute_speed_limit(road) # process the path of each road, and the corresponding shapely geometry. for road in Road.roads: road.process_path() road.smooth_sharp_angles() road.process_shape() # process shapely geometry for each crossroad, and substract them to the road paths. for crossroad in cls.crossroads.values(): crossroad.process_shape() crossroad.cut_roads() @classmethod def generate_unique_road_id(cls, osmid): """Create a new road unique ID.""" counter = 0 while True: counter += 1 candidate = '%s_%d' % (osmid, counter) unique = True for road in cls.roads: if road.id == candidate: unique = False break if unique: return candidate @classmethod def are_coords_close_to_some_road_waypoint(cls, coordinates, areOSMReferences=True): """Return if a list of coordinates close to some road waypoints.""" if areOSMReferences: for ref in coordinates: if ref in OSMCoord.coordDictionnary: coordPos = [ OSMCoord.coordDictionnary[ref].x, OSMCoord.coordDictionnary[ref].z ] if Road.is_coord_close_to_some_road_waypoint_way_point( coordPos): return True else: for ref in coordinates: if Road.is_coord_close_to_some_road_waypoint_way_point( [ref[0], ref[1]]): return True return False @classmethod def is_coord_close_to_some_road_waypoint_way_point(cls, pos): """Check if the given pos is close from one road waypoint.""" for roadWayPoint in cls.allRoadWayPoints: if math.sqrt( math.pow(roadWayPoint[0] - pos[0], 2) + math.pow(roadWayPoint[1] - pos[1], 2)) < Road.removalRadius: return True return False @classmethod def _convert_webots_to_osm_coord(cls, coord): return [ -coord[0] + WebotsObject.xOffset, coord[1] + WebotsObject.zOffset ]
def accept(self): print 'run' pointLayer = processing.getObject(str(self.uPointLayer.currentText())) # check for selected features if self.uSelectedPoints.isChecked(): pointSelectFlag = True else: pointSelectFlag = False if self.uSelectedLine.isChecked(): lineSelectFlag = True else: lineSelectFlag = False ## check if need to build line if self.utabWidget.currentIndex()==0: lineLayer = self.buildLine(pointSelectFlag) if lineLayer=='Error': return else: lineLayer = processing.getObject(str(self.uLineLayer.currentText())) zField = self.uZfield.currentText() pointIdField = self.uPointID.currentText() try: noData = float(self.lineEditNoData.text()) except: QMessageBox.warning(self,'Error', 'No data value must be numeric') return if self.uBuffer.displayText() !='': buff=float(self.uBuffer.displayText()) else: buff=None # trap for coordinate system if lineLayer.crs()!=pointLayer.crs(): QMessageBox.warning(self,'Error', 'Point layer coordinate system does not match line coordinate system') return # trap for more than one line feature counter = 0 if lineSelectFlag: lineFeatureList = lineLayer.selectedFeatures() else: lineFeatureList = lineLayer.getFeatures() for lineFeatures in lineFeatureList: counter = counter+1 if counter!=1: QMessageBox.warning(self,'Error', 'More than one line feature in line layer') return if lineSelectFlag: lineFeat = lineLayer.selectedFeatures()[0] else: lineFeat = lineLayer.getFeatures().next() lineGeom = lineFeat.geometry() lineShap = LineString(lineGeom.asPolyline()) if buff: lineBoundary = lineShap.buffer(buff) if pointSelectFlag: pointFeatureList = pointLayer.selectedFeatures() else: pointFeatureList = pointLayer.getFeatures() pointList = [] for pointFeature in pointFeatureList: pointGeom = pointFeature.geometry() pointShap = Point(pointGeom.asPoint()) if buff: ## check if point is within line buffer if pointShap.within(lineBoundary): z = pointFeature[zField] ### get distance along line dist = lineShap.project(pointShap) pointId = pointFeature[pointIdField] ##store data pointList.append([dist, z, pointId]) else: z = pointFeature[zField] ### get distance along line dist = lineShap.project(pointShap) pointId = pointFeature[pointIdField] ##store data pointList.append([dist, z, pointId]) ###sort data by distance pointList = sorted(pointList, key=itemgetter(0)) ###seperate data back into individual lists zList = [] distList = [] pointIdList = [] for i in pointList: ## only keep data that is not flaged as noData if i[1]!=noData: distList.append(i[0]) zList.append(i[1]) pointIdList.append(i[2]) self.values = [distList, zList, pointIdList] self.refreshPlot() self.uCopytoClip.setEnabled(True)
def main(): #-- Read the system arguments listed after the program long_options = [ 'subdir=', 'method=', 'step=', 'indir=', 'interval=', 'buffer=', 'manual' ] optlist, arglist = getopt.getopt(sys.argv[1:], '=D:M:S:I:V:B:m:', long_options) subdir = 'all_data2_test' method = '' step = 50 n_interval = 1000 buffer_size = 500 indir = '' set_manual = False for opt, arg in optlist: if opt in ('-D', '--subdir'): subdir = arg elif opt in ('-M', '--method'): method = arg elif opt in ('-S', '--step'): step = np.int(arg) elif opt in ('-V', '--interval'): n_interval = np.int(arg) elif opt in ('-B', '--buffer'): buffer_size = np.int(arg) elif opt in ('-I', '--indir'): indir = os.path.expanduser(arg) elif opt in ('-m', '--manual'): set_manual = True #-- directory setup #- current directory current_dir = os.path.dirname(os.path.realpath(__file__)) headDirectory = os.path.join(current_dir, '..', 'FrontLearning_data') glaciersFolder = os.path.join(headDirectory, 'Glaciers') results_dir = os.path.join(headDirectory, 'Results', subdir) #-- if user input not given, set label folder #-- else if input directory is given, then set the method based on that if indir == '': indir = os.path.join(results_dir, method, method) else: method = os.path.basename(indir) if method == '': sys.exit("Please do not put '/' at the end of indir.") print('input directory ONLY for NN output:%s' % indir) print('METHOD:%s' % method) #-- make histohtam filder if it doesn't exist histFolder = os.path.join(results_dir, 'Histograms') if (not os.path.isdir(histFolder)): os.mkdir(histFolder) outputFolder = os.path.join( histFolder, method + '_' + str(step) + '_%isegs' % n_interval + '_%ibuffer' % buffer_size) #-- make output folders if (not os.path.isdir(outputFolder)): os.mkdir(outputFolder) if set_manual: datasets = ['NN', 'Sobel', 'Manual'] else: datasets = ['NN', 'Sobel'] print(datasets) pixelFolder = {} frontFolder = {} pixelFolder['NN'] = os.path.join(results_dir, method, method + ' Pixel CSVs ' + str(step)) pixelFolder['Sobel'] = os.path.join(results_dir, 'Sobel/Sobel Pixel CSVs ' + str(step)) if 'Manual' in datasets: pixelFolder['Manual'] = os.path.join( results_dir, 'output_handrawn/output_handrawn Pixel CSVs ' + str(step)) frontFolder['NN'] = os.path.join(results_dir, method, method + ' Geo CSVs ' + str(step)) frontFolder['Sobel'] = os.path.join(results_dir, 'Sobel/Sobel Geo CSVs ' + str(step)) if 'Manual' in datasets: frontFolder['Manual'] = os.path.join( results_dir, 'output_handrawn/output_handrawn Geo CSVs ' + str(step)) def seriesToNPoints(series, N): #find the total length of the series totalDistance = 0 for s in range(len(series[:, 0]) - 1): totalDistance += ((series[s, 0] - series[s + 1, 0])**2 + (series[s, 1] - series[s + 1, 1])**2)**0.5 intervalDistance = totalDistance / (N - 1) #make the list of points newSeries = series[0, :] currentS = 0 currentPoint1 = series[currentS, :] currentPoint2 = series[currentS + 1, :] for p in range(N - 2): distanceAccrued = 0 while distanceAccrued < intervalDistance: currentLineDistance = ( (currentPoint1[0] - currentPoint2[0])**2 + (currentPoint1[1] - currentPoint2[1])**2)**0.5 if currentLineDistance < intervalDistance - distanceAccrued: distanceAccrued += currentLineDistance currentS += 1 currentPoint1 = series[currentS, :] currentPoint2 = series[currentS + 1, :] else: distance = intervalDistance - distanceAccrued newX = currentPoint1[0] + ( distance / currentLineDistance) * (currentPoint2[0] - currentPoint1[0]) newY = currentPoint1[1] + ( distance / currentLineDistance) * (currentPoint2[1] - currentPoint1[1]) distanceAccrued = intervalDistance + 1 newSeries = np.vstack([newSeries, np.array([newX, newY])]) currentPoint1 = np.array([newX, newY]) newSeries = np.vstack([newSeries, series[-1, :]]) return (newSeries) def frontComparisonErrors(front1, front2): errors = [] for ff in range(len(front1)): dist = ((front1[ff, 0] - front2[ff, 0])**2 + (front1[ff, 1] - front2[ff, 1])**2)**0.5 errors.append(dist) return (errors) def rmsError(error): return (np.sqrt(np.mean(np.square(error)))) def generateLabelList(labelFolder): labelList = [] for fil in os.listdir(labelFolder): # if fil[-6:] == 'B8.png' or fil[-6:] == 'B2.png': # labelList.append(fil[:-4]) if fil.endswith('_nothreshold.png'): labelList.append(fil.replace('_nothreshold.png', '')) return (labelList) # get glacier names def getGlacierList(labelList): f = open(os.path.join(glaciersFolder, 'Scene_Glacier_Dictionary.csv'), 'r') lines = f.read() f.close() lines = lines.split('\n') glacierList = [] for sceneID in labelList: for line in lines: line = line.split(',') if line[0] == sceneID: glacierList.append(line[1]) return (glacierList) #code to get the list of fronts and their images def getFrontList(glacierList, labelList): frontsList = [] for ind, label in enumerate(labelList): glacier = glacierList[ind] f = open( os.path.join(glaciersFolder, glacier, '%s Image Data.csv' % glacier), 'r') lines = f.read() f.close() lines = lines.split('\n') for line in lines: line = line.split(',') if line[1][:-4] == label: frontsList.append(line[0]) return (frontsList) def fjordBoundaryIndices(glacier): boundary1file = os.path.join(glaciersFolder, glacier, 'Fjord Boundaries', glacier + ' Boundary 1 V2.csv') boundary1 = np.genfromtxt(boundary1file, delimiter=',') boundary2file = os.path.join(glaciersFolder, glacier, 'Fjord Boundaries', glacier + ' Boundary 2 V2.csv') boundary2 = np.genfromtxt(boundary2file, delimiter=',') boundary1 = seriesToNPoints(boundary1, 1000) boundary2 = seriesToNPoints(boundary2, 1000) return (boundary1, boundary2) labelList = generateLabelList(indir) glacierList = getGlacierList(labelList) frontList = getFrontList(glacierList, labelList) allerrors = {} allerrors['NN'] = [] allerrors['Sobel'] = [] allerrors['Manual'] = [] N = 1 N = len(labelList) for ll in range(N): glacier = glacierList[ll] label = labelList[ll] trueFrontFile = frontList[ll] print(label) ############################################################################ # This section to get the front images trueImageFolder = os.path.join(headDirectory, 'Glaciers', glacier, 'Small Images') trueImage = Image.open( os.path.join(trueImageFolder, label + '_Subset.png')).transpose( Image.FLIP_LEFT_RIGHT).convert("L") frontImageFolder = {} frontImageFolder['NN'] = indir frontImageFolder['Sobel'] = os.path.join(results_dir, 'Sobel/Sobel') if 'Manual' in datasets: frontImageFolder['Manual'] = os.path.join(os.path.dirname(indir), 'output_handrawn') frontImage = {} pixels = {} for d, tl in zip(datasets, ['_nothreshold', '', '_nothreshold']): frontImage[d] = Image.open(os.path.join(frontImageFolder[d],label \ + '%s.png'%tl)).transpose(Image.FLIP_LEFT_RIGHT).convert("L") ############################################################################ # This section to get the front pixels # get the front pixelsFile = glacier + ' ' + label + ' Pixels.csv' pixels[d] = np.genfromtxt(os.path.join(pixelFolder[d], pixelsFile), delimiter=',') pixels[d] = seriesToNPoints(pixels[d], n_interval) ############################################################################ # Get the fjord boundaries for the current glacier bounds = {} bounds[1], bounds[2] = fjordBoundaryIndices(glacier) buff = {} for i in [1, 2]: # Form buffer around boundary lineStringSet = bounds[i] line = LineString(lineStringSet) buff[i] = line.buffer(buffer_size) ############################################################################ # This section to get the front data #get the true front trueFrontFolder = os.path.join(glaciersFolder, glacier, 'Front Locations', '3413') trueFront = np.genfromtxt(trueFrontFolder + '/' + trueFrontFile, delimiter=',') #-- make sure all fronts go in the same direction #-- if the x axis is not in increasng order, reverse if trueFront[0, 0] > trueFront[-1, 0] and glacier != 'Helheim': print('flipped true front.') trueFront = trueFront[::-1, :] trueFront = seriesToNPoints(trueFront, n_interval) #-- get rid of poitns too close to the edges l1 = LineString(trueFront) int1 = l1.difference(buff[1]) int2 = int1.difference(buff[2]) try: trueFront = np.array(shape(int2).coords) except: lengths = [ len(np.array(shape(int2)[i].coords)) for i in range(len(shape(int2))) ] max_ind = np.argmax(lengths) trueFront = np.array(shape(int2)[max_ind].coords) #-- testing print(lengths) print(lengths[max_ind]) #-- rebreak into n_interval segments trueFront = seriesToNPoints(trueFront, n_interval) front = {} errors = {} for d in datasets: #get the front frontFile = glacier + ' ' + label + ' Profile.csv' temp_front = np.genfromtxt(os.path.join(frontFolder[d], frontFile), delimiter=',') #-- make sure all fronts go in the same direction #-- if the x axis is not in increasng order, reverse #if temp_front[0,0] > temp_front[-1,0]: # print('flipped %s'%d) # temp_front = temp_front[::-1,:] front[d] = seriesToNPoints(temp_front, n_interval) #-- get rid of points to close to the edges #-- get rid of poitns too close to the edges l1 = LineString(front[d]) int1 = l1.difference(buff[1]) int2 = int1.difference(buff[2]) try: front[d] = np.array(shape(int2).coords) except: lengths = [ len(np.array(shape(int2)[i].coords)) for i in range(len(shape(int2))) ] max_ind = np.argmax(lengths) front[d] = np.array(shape(int2)[max_ind].coords) #-- testing print(lengths) print(lengths[max_ind]) #-- rebreak into n_interval segments front[d] = seriesToNPoints(front[d], n_interval) errors[d] = frontComparisonErrors(trueFront, front[d]) for error in errors[d]: allerrors[d].append(error) #-- plot fronts for debugging purposes -- double checking. # plt.plot(trueFront[:,0],trueFront[:,1],label='True') # plt.plot(front['NN'][:,0],front['NN'][:,1,],label='NN') # plt.legend() # plt.show() frontXmin = np.min( np.concatenate(([np.min(trueFront[:, 0])], [np.min(front[d][:, 0]) for d in datasets]))) frontXmax = np.max( np.concatenate(([np.max(trueFront[:, 0])], [np.max(front[d][:, 0]) for d in datasets]))) frontYmin = np.min( np.concatenate(([np.min(trueFront[:, 1])], [np.min(front[d][:, 1]) for d in datasets]))) frontYmax = np.max( np.concatenate(([np.max(trueFront[:, 1])], [np.max(front[d][:, 1]) for d in datasets]))) fig = plt.figure(figsize=(10, 8)) n_panels = len(front) + 1 plt.subplot(2, n_panels, 1) plt.imshow(trueImage, cmap='gray') plt.gca().set_xlim([0, 200]) plt.gca().set_ylim([300, 0]) plt.gca().axes.get_xaxis().set_ticks([]) plt.gca().axes.get_yaxis().set_ticks([]) plt.title('Original Image', fontsize=12) p = 2 for d in datasets: plt.subplot(2, n_panels, p) plt.imshow(frontImage[d], cmap='gray') plt.plot(pixels[d][:, 0], pixels[d][:, 1], 'y-', linewidth=3) plt.gca().set_xlim([0, 200]) plt.gca().set_ylim([300, 0]) plt.gca().axes.get_xaxis().set_ticks([]) plt.gca().axes.get_yaxis().set_ticks([]) plt.title('%s Solution' % d, fontsize=12) p += 1 plt.subplot(2, n_panels, p) plt.title('Geocoded Solutions', fontsize=12) plt.ylabel('Northing (km)', fontsize=12) plt.xlabel('Easting (km)', fontsize=12) plt.plot(trueFront[:, 0] / 1000, trueFront[:, 1] / 1000, 'k-', label='True') for d, c in zip(datasets, ['b-', 'g-', 'r-']): plt.plot(front[d][:, 0] / 1000, front[d][:, 1] / 1000, c, label=d) plt.gca().set_xlim([frontXmin / 1000, frontXmax / 1000]) plt.gca().set_ylim([frontYmin / 1000, frontYmax / 1000]) plt.gca().set_xticks([frontXmin / 1000, frontXmax / 1000]) plt.gca().set_yticks([frontYmin / 1000, frontYmax / 1000]) plt.legend(loc=0) p += 1 p_temp = copy.copy(p) x = {} y = {} for d, c in zip(datasets, ['b', 'g', 'r']): plt.subplot(2, n_panels, p) plt.title('%s Errors Histogram' % d, fontsize=12) bins = range(0, 5000, 100) y[d], x[d], _ = plt.hist(errors[d], alpha=0.5, color=c, bins=bins, label='NN') #plt.xlabel('RMS Error = '+'{0:.2f}'.format(rmsError(errors[d]))+' m',fontsize=12) plt.xlabel('Mean Diff. = ' + '{0:.2f}'.format(np.mean(np.abs(errors[d]))) + ' m', fontsize=12) p += 1 #-- set histogram bounds for d in datasets: plt.subplot(2, n_panels, p_temp) plt.gca().set_ylim([0, np.max([y[d] for d in datasets])]) plt.gca().set_xlim([0, np.max([x[d] for d in datasets])]) p_temp += 1 plt.savefig(os.path.join(outputFolder, label + '.png'), bbox_inches='tight') plt.close(fig) fig = plt.figure(figsize=(11, 4)) x = {} y = {} for i, d, c, lbl in zip(range(len(datasets)), datasets, ['b', 'g', 'r'], ['e', 'f', 'g']): plt.subplot(1, len(datasets), i + 1) plt.title(r"$\bf{%s)}$" % lbl + " %s Error Histogram" % d, fontsize=12) bins = range(0, 5000, 100) y[d], x[d], _ = plt.hist(allerrors[d], alpha=0.5, color=c, bins=bins, label=d) #plt.xlabel('RMS Error = '+'{0:.2f}'.format(rmsError(allerrors[d]))+' m',fontsize=12) plt.xlabel('Mean Difference = ' + '{0:.2f}'.format(np.mean(np.abs(allerrors[d]))) + ' m', fontsize=12) if i == 0: plt.ylabel('Count (100 m bins)', fontsize=12) for i in range(len(datasets)): plt.subplot(1, len(datasets), i + 1) plt.gca().set_ylim([0, np.max([y[d] for d in datasets])]) plt.gca().set_xlim([0, np.max([x[d] for d in datasets])]) plt.savefig(os.path.join(results_dir,\ 'Figure_4_'+'_'.join(method.split())+'_'+str(step)+'_%isegs'%n_interval+'_%ibuffer'%buffer_size+'.pdf'),bbox_inches='tight') plt.close()
print(polysquare.contains(Point(1,1))) #test if point is within the polygon print(Point(1,1).within(polysquare)) #test if other point is within the polygon print(Point(5,7).within(polysquare)) """ Buffer example """ #import linestring class, cap and join styles from shapely.geometry import LineString, CAP_STYLE, JOIN_STYLE unbufferedLine = LineString([(0, 0), (1, 1), (0, 2), (2, 2), (3, 1), (1, 0)]) dilatedBufferedLine = unbufferedLine.buffer(0.5, cap_style = CAP_STYLE.square) erodedBufferedLine = dilatedBufferedLine.buffer(-0.3, join_style = JOIN_STYLE.round) #Show the polygon detail. This gets more complicated than a line! print(erodedBufferedLine) """ Simplify example """ #print area of polygon print(erodedBufferedLine.area) #show number of coordinates needed to define exterior of the polygon print(len(erodedBufferedLine.exterior.coords)) #simplify erodedSimplified = erodedBufferedLine.simplify(0.05, preserve_topology=False)
def main(): config = load_config() flows_file = os.path.join(config['paths']['data'], 'Results', 'Failure_shapefiles', 'weighted_edges_failures_national_rail_2.shp') plot_sets = [{ 'file_tag': 'reroute', 'no_access': [-1, 0], 'legend_label': "(million USD/day)", 'divisor': 1000000, 'columns': ['min_tr_los', 'max_tr_los'], 'title_cols': ['Rerouting costs (min)', 'Rerouting costs (max)'] }, { 'file_tag': 'commodities', 'no_access': [-1, 1], 'legend_label': "AADF (tons/day)", 'divisor': 1, 'columns': ['min_tons', 'max_tons'], 'title_cols': ['Total tonnage (min)', 'Total tonnage (max)'] }, { 'file_tag': 'economic', 'no_access': [-1, 1], 'legend_label': "(million USD/day)", 'divisor': 1000000, 'columns': ['min_econ_l', 'max_econ_l'], 'title_cols': ['Economic losses (min)', 'Economic losses (max)'] }, { 'file_tag': 'total', 'no_access': [0, 1], 'legend_label': "(million USD/day)", 'divisor': 1000000, 'columns': ['min_loss', 'max_loss'], 'title_cols': ['Total economic impact (min)', 'Total economic impact (max)'] }] color = '#006d2c' color_by_type = {'Rail Line': color} for plot_set in plot_sets: for c in range(len(plot_set['columns'])): ax = get_axes() plot_basemap(ax, config['paths']['data'], highlight_region=[]) scale_bar(ax, location=(0.8, 0.05)) plot_basemap_labels(ax, config['paths']['data']) proj_lat_lon = ccrs.PlateCarree() column = plot_set['columns'][c] weights = [ record.attributes[column] for record in shpreader.Reader(flows_file).records() if int(record.attributes['no_access']) in plot_set['no_access'] ] max_weight = max(weights) width_by_range = generate_weight_bins(weights) geoms_by_range = {} for value_range in width_by_range: geoms_by_range[value_range] = [] for record in [ rec for rec in shpreader.Reader(flows_file).records() if int(rec.attributes['no_access']) in plot_set['no_access'] ]: val = record.attributes[column] geom = record.geometry for nmin, nmax in geoms_by_range: if nmin <= val and val < nmax: geoms_by_range[(nmin, nmax)].append(geom) # plot for range_, width in width_by_range.items(): ax.add_geometries( [geom.buffer(width) for geom in geoms_by_range[range_]], crs=proj_lat_lon, edgecolor='none', facecolor=color, zorder=2) x_l = 102.3 x_r = x_l + 0.4 base_y = 14 y_step = 0.4 y_text_nudge = 0.1 x_text_nudge = 0.1 ax.text(x_l, base_y + y_step - y_text_nudge, plot_set['legend_label'], horizontalalignment='left', transform=proj_lat_lon, size=10) divisor = plot_set['divisor'] for (i, ((nmin, nmax), width)) in enumerate(width_by_range.items()): y = base_y - (i * y_step) line = LineString([(x_l, y), (x_r, y)]) ax.add_geometries([line.buffer(width)], crs=proj_lat_lon, linewidth=0, edgecolor=color, facecolor=color, zorder=2) if nmin == max_weight: label = '>{:.2f}'.format(max_weight / divisor) else: label = '{:.2f}-{:.2f}'.format(nmin / divisor, nmax / divisor) ax.text(x_r + x_text_nudge, y - y_text_nudge, label, horizontalalignment='left', transform=proj_lat_lon, size=10) plt.title(plot_set['title_cols'][c], fontsize=14) output_file = os.path.join( config['paths']['figures'], 'rail_failure-map-{}-{}.png'.format(plot_set['file_tag'], column)) save_fig(output_file) plt.close()
del subConvexPathList[line] pairList = [] for i in range(len(subVertices)): for j in range(i+1, len(subVertices)): pairList.append((subVertices[i], subVertices[j])) for i in pairList: Line = LineString(i) chk_cross = 0 for obs in containingObs: if Line.crosses(obs): chk_cross = 1 elif Line.within(obs): chk_cross = 1 if chk_cross == 0: subConvexPathList[i] = Line buffer_st_line = split_line.buffer(0.1) deleteList = [] for line in subConvexPathList: if buffer_st_line.contains(LineString(line)): deleteList.append(line) for line in deleteList: if subConvexPathList.has_key(line): del subConvexPathList[line] for line in subConvexPathList: if not totalConvexPathList.has_key(line): totalConvexPathList[line] = subConvexPathList[line] labeled_multyPoly = MultiPolygon([x for x in labeledObstaclePoly]) convexHull = createConvexhull(labeled_multyPoly, odPointsList) splitBoundary(totalConvexPathList, convexHull)
# Which stations adjoin the bus- and raillines ptShapesBus = busStationGeom.shapes() linShapesBus = busLineGeom.shapes() lineNrBus = 0 ptShapesRail = railStationGeom.shapes() linShapesRail = railLineGeom.shapes() lineNrRail = 0 for line in linShapesBus: lineS = LineString(line.points) pointNr = 0 if str(records_busLineList[lineNrBus][0]) <> '': for point in ptShapesBus: pointS = Point(point.points[0]) pp = "" if lineS.buffer(0.0004).intersects(pointS): part = float(lineS.project(pointS)) / float(lineS.length) # Geometry transform function based on pyproj.transform (in case length is in degree an not in meters project = partial(pyproj.transform, pyproj.Proj(init='EPSG:4326'), pyproj.Proj(init='EPSG:21781')) line2 = transform(project, lineS) point2 = transform(project, pointS) pp = line2.project(point2) # Create a new feature (attribute and geometry) feature = ogr.Feature(layer.GetLayerDefn()) feature.SetField('oid', lineNrBus) feature.SetField('lin_nr', str(records_busLineList[lineNrBus][0])) feature.SetField('lin_name',
implementation = platform.python_implementation() print('implementation: {}'.format(implementation)) # SvgTestCase.test_collection is failing due to GEOS 3.9 different coordinate order # (it's fixed on master so can be removed again with Shapely 1.8) pytest_args = ['tests', '-k', 'not test_collection'] if implementation != 'PyPy': from shapely import speedups import shapely.speedups._speedups import shapely.vectorized import shapely.vectorized._vectorized assert speedups.available speedups.enable() pytest_args.append('--with-speedups') py.test.cmdline.main(pytest_args) from shapely.geometry import LineString ls = LineString([(0, 0), (10, 0)]) # On OSX causes an abort trap, due to https://github.com/Toblerity/Shapely/issues/177 r = ls.wkt area = ls.buffer(10).area # Check if we can import lgeos. # https://github.com/conda-forge/shapely-feedstock/issues/17 from shapely.geos import lgeos
def main(): config = load_config() output_file = os.path.join(config['paths']['figures'], 'coastal-map.png') coastal_edge_file_path = os.path.join(config['paths']['data'], 'post_processed_networks', 'coastal_edges.shp') coastal_flow_file_path = os.path.join( config['paths']['output'], 'flow_mapping_combined', 'weighted_flows_national_coastal_100_percent.csv') coastal_node_file = os.path.join(config['paths']['data'], 'post_processed_networks', 'coastal_nodes.shp') coastal_edge_file = gpd.read_file(coastal_edge_file_path, encoding='utf-8') coastal_flow_file = pd.read_csv(coastal_flow_file_path) coastal_edge_file = pd.merge(coastal_edge_file, coastal_flow_file, how='left', on=['edge_id']).fillna(0) color = '#045a8d' color_by_type = {'coastal Line': color} crop_cols = [ 'max_rice', 'max_cash', 'max_cass', 'max_teas', 'max_maiz', 'max_rubb', 'max_swpo', 'max_acof', 'max_rcof', 'max_pepp' ] ind_cols = [ 'max_sugar', 'max_wood', 'max_steel', 'max_constructi', 'max_cement', 'max_fertilizer', 'max_coal', 'max_petroluem', 'max_manufactur', 'max_fishery', 'max_meat', 'max_tons' ] columns = crop_cols + ind_cols column_label_divisors = {c: 1000 for c in columns} legend_label = "AADF ('000 tons/day)" title_cols = [ 'Rice', 'Cashew', 'Cassava', 'Teas', 'Maize', 'Rubber', 'Sweet Potatoes', 'Coffee Arabica', 'Coffee Robusta', 'Pepper', 'Sugar', 'Wood', 'Steel', 'Construction materials', 'Cement', 'Fertilizer', 'Coal', 'Petroleum', 'Manufacturing', 'Fishery', 'Meat', 'Total tonnage' ] for c in range(len(columns)): ax = get_axes() plot_basemap(ax, config['paths']['data'], highlight_region=[]) scale_bar(ax, location=(0.8, 0.05)) plot_basemap_labels(ax, config['paths']['data']) proj_lat_lon = ccrs.PlateCarree() column = columns[c] weights = [ record['max_tons'] for iter_, record in coastal_edge_file.iterrows() ] max_weight = max(weights) width_by_range = generate_weight_bins(weights) geoms_by_range = {} for value_range in width_by_range: geoms_by_range[value_range] = [] for iter_, record in coastal_edge_file.iterrows(): val = record[column] geom = record.geometry if val > 0: # only add edges that carry this commodity for nmin, nmax in geoms_by_range: if nmin <= val and val < nmax: geoms_by_range[(nmin, nmax)].append(geom) # plot for range_, width in width_by_range.items(): ax.add_geometries( [geom.buffer(width) for geom in geoms_by_range[range_]], crs=proj_lat_lon, edgecolor='none', facecolor=color, zorder=2) x_l = 102.3 x_r = x_l + 0.4 base_y = 14 y_step = 0.4 y_text_nudge = 0.1 x_text_nudge = 0.1 ax.text(x_l, base_y + y_step - y_text_nudge, legend_label, horizontalalignment='left', transform=proj_lat_lon, size=10) divisor = column_label_divisors[column] for (i, ((nmin, nmax), width)) in enumerate(width_by_range.items()): y = base_y - (i * y_step) line = LineString([(x_l, y), (x_r, y)]) ax.add_geometries([line.buffer(width)], crs=proj_lat_lon, linewidth=0, edgecolor=color, facecolor=color, zorder=2) if nmin == max_weight: label = '>{:.2f}'.format(max_weight / divisor) else: label = '{:.2f}-{:.2f}'.format(nmin / divisor, nmax / divisor) ax.text(x_r + x_text_nudge, y - y_text_nudge, label, horizontalalignment='left', transform=proj_lat_lon, size=10) plt.title(title_cols[c], fontsize=14) output_file = os.path.join( config['paths']['figures'], 'coastal_flow-map-{}-max-scale.png'.format(column)) save_fig(output_file) plt.close()
GoodPoints[i] = Point(lon_val, lat_val) GoodPath1 = LineString(GoodPoints[0:break1_ind + 1]) GoodPath2 = LineString(GoodPoints[break1_ind + 1:break2_ind + 1]) GoodPath3 = LineString(GoodPoints[break2_ind + 1:break3_ind + 1]) GoodPath4 = LineString(GoodPoints[break3_ind + 1:]) ## Iterate through buffer sizes buff1 = 1 buff2 = 2.3 buff3 = 1 buff4 = 1.6 BoundaryCurrent1 = GoodPath1.buffer(buff1) BoundaryCurrent2 = GoodPath2.buffer(buff2) BoundaryCurrent3 = GoodPath3.buffer(buff3) BoundaryCurrent4 = GoodPath4.buffer(buff4) x1, y1 = BoundaryCurrent1.exterior.xy x2, y2 = BoundaryCurrent2.exterior.xy x3, y3 = BoundaryCurrent3.exterior.xy x4, y4 = BoundaryCurrent4.exterior.xy bc_included_bc = 0 gyre_included_bc = 0 # Determine what percentage of points fall in boundary current for i in np.arange(len(All_Lat_BC)): p = Point(All_Lon_BC[i], All_Lat_BC[i])
spots, points, lines = [], [], [] buffer_distance = 0.0001 for feature in js['features']: if feature['geometry'] and feature['geometry']['type'] == 'Point': x = feature['geometry']['coordinates'][0] y = feature['geometry']['coordinates'][1] p = Point(x, y) points.append(p) spots.append(p.buffer(buffer_distance)) for feature in js['features']: if feature['geometry'] and feature['geometry']['type'] == 'LineString': l = LineString(feature['geometry']['coordinates']) lines.append(l) spots.append(l.buffer(buffer_distance)) patches = cascaded_union(spots) # see also: object.symmetric_difference(other) import copy patches = [] for spot1 in spots: sfinal = copy.deepcopy(spot1) # for spot2 in spots: # if spot1 != spot2: # sfinal = spot1.difference(spot2) patches.append(sfinal) features = [] for p in patches:
class Creature: """ Generates a complete virtual creature Tests ----------- """ def __init__(self, params): """ Initialises a simple L-system Parameters ---------- variables : str a string containing all of the letters that take part in the recursion. These letters should also have associated rules. constants : str or None a string containing all the letters that do not take part in the recursion. These letters will not have an associated rule axiom : str The initial character string rules : dict a dictionary containing the rules for recursion. This is a dictionary of listing the letter replacement in the recursion. eg. {"A": "AB", "B": "A"} """ if not 'L-string' in params: self.rules = params.get('rules') self.l_string = params.get('axiom') self.constants = params.get('constants') self.variables = params.get('variables') self.recur(params.get('num_char')) else: self.l_string = params.get('L-string') self.length = params.get('length') self.angle = radians(params.get('angle')) self.mapper() self.layout() def recur(self, n): for _ in range(n): self.l_string = ''.join([self.next_char(c) for c in self.l_string]) self.l_string = self.l_string.replace('X', '') def next_char(self, c): rule = self.rules.get(c, c) if len(rule) > 1: return np.random.choice(self.rules.get(c, ["Other"])["options"], p=self.rules.get( c, ["Other"])["probabilities"]) else: return rule def mapper(self): """Converts L-string to coordinates Returns ------- List List of coordinates """ num_chars = len(self.l_string) coords = np.zeros((num_chars + 1, 3), np.double) rotVec = np.array(((cos(self.angle), -sin(self.angle), 0), (sin(self.angle), cos(self.angle), 0), (0, 0, 1))) start_vec = np.array((0, 1, 0), np.float64) curr_vec = start_vec i = 1 for c in self.l_string: if c == 'F': coords[i] = (coords[i - 1] + (self.length * curr_vec)) i += 1 if c == '-': curr_vec = np.dot(curr_vec, (-1 * rotVec)) if c == '+': curr_vec = np.dot(curr_vec, rotVec) coords = np.delete(coords, np.s_[i:], 0) self.coords = coords def layout(self): self.linestring = LineString(self.coords[:, :2]) self.area = self.linestring.buffer(0.499999).area self.bounds = self.linestring.bounds
def main(): config = load_config() data_path = config['paths']['data'] coastal_edge_file_path = os.path.join(config['paths']['data'], 'network', 'port_edges.shp') coastal_flow_file_path = os.path.join( config['paths']['output'], 'flow_mapping_combined', 'weighted_flows_port_100_percent.csv') coastal_node_file = os.path.join(config['paths']['data'], 'network', 'port_nodes.shp') mode_file = gpd.read_file(coastal_edge_file_path, encoding='utf-8') flow_file = pd.read_csv(coastal_flow_file_path, encoding='utf-8-sig') mode_file = pd.merge(mode_file, flow_file, how='left', on=['edge_id']).fillna(0) color = '#045a8d' color_by_type = {'coastal Line': color} plot_sets = [ { 'file_tag': 'commodities', 'legend_label': "AADF ('000 tons/day)", 'divisor': 1000, 'columns': [ 'max_{}'.format(x) for x in [ 'total_tons', 'AGRICULTURA, GANADERÍA, CAZA Y SILVICULTURA', 'COMERCIO', 'EXPLOTACIÓN DE MINAS Y CANTERAS', 'INDUSTRIA MANUFACTURERA', 'PESCA', 'TRANSPORTE Y COMUNICACIONES' ] ], 'title_cols': [ 'Total tonnage', 'AGRICULTURA, GANADERÍA, CAZA Y SILVICULTURA', 'COMERCIO', 'EXPLOTACIÓN DE MINAS Y CANTERAS', 'INDUSTRIA MANUFACTURERA', 'PESCA', 'TRANSPORTE Y COMUNICACIONES' ], 'significance': 0 }, ] for plot_set in plot_sets: for c in range(len(plot_set['columns'])): # basemap proj_lat_lon = ccrs.PlateCarree() ax = get_axes() plot_basemap(ax, data_path) scale_bar(ax, location=(0.8, 0.05)) plot_basemap_labels(ax, data_path, include_regions=True) column = 'max_total_tons' weights = [ record[column] for iter_, record in mode_file.iterrows() ] max_weight = max(weights) width_by_range = generate_weight_bins(weights, n_steps=7, width_step=0.02) geoms_by_range = {} for value_range in width_by_range: geoms_by_range[value_range] = [] column = plot_set['columns'][c] for iter_, record in mode_file.iterrows(): val = record[column] geom = record.geometry for nmin, nmax in geoms_by_range: if nmin <= val and val < nmax: geoms_by_range[(nmin, nmax)].append(geom) # plot for range_, width in width_by_range.items(): ax.add_geometries( [geom.buffer(width) for geom in geoms_by_range[range_]], crs=proj_lat_lon, edgecolor='none', facecolor=color, zorder=2) x_l = -58.4 x_r = x_l + 0.4 base_y = -45.1 y_step = 0.8 y_text_nudge = 0.2 x_text_nudge = 0.2 ax.text(x_l, base_y + y_step - y_text_nudge, plot_set['legend_label'], horizontalalignment='left', transform=proj_lat_lon, size=10) divisor = plot_set['divisor'] significance_ndigits = plot_set['significance'] max_sig = [] for (i, ((nmin, nmax), line_style)) in enumerate(width_by_range.items()): if round(nmin / divisor, significance_ndigits) < round( nmax / divisor, significance_ndigits): max_sig.append(significance_ndigits) elif round(nmin / divisor, significance_ndigits + 1) < round( nmax / divisor, significance_ndigits + 1): max_sig.append(significance_ndigits + 1) elif round(nmin / divisor, significance_ndigits + 2) < round( nmax / divisor, significance_ndigits + 2): max_sig.append(significance_ndigits + 2) else: max_sig.append(significance_ndigits + 3) significance_ndigits = max(max_sig) for (i, ((nmin, nmax), width)) in enumerate(width_by_range.items()): y = base_y - (i * y_step) line = LineString([(x_l, y), (x_r, y)]) ax.add_geometries([line.buffer(width)], crs=proj_lat_lon, linewidth=0, edgecolor=color, facecolor=color, zorder=2) if nmin == max_weight: value_template = '>{:.' + str(significance_ndigits) + 'f}' label = value_template.format( round(max_weight / divisor, significance_ndigits)) else: value_template = '{:.' + str(significance_ndigits) + \ 'f}-{:.' + str(significance_ndigits) + 'f}' label = value_template.format( round(nmin / divisor, significance_ndigits), round(nmax / divisor, significance_ndigits)) ax.text(x_r + x_text_nudge, y - y_text_nudge, label, horizontalalignment='left', transform=proj_lat_lon, size=10) plt.title('Max AADF - {}'.format(plot_set['title_cols'][c]), fontsize=10) output_file = os.path.join( config['paths']['figures'], 'water_flow-map-{}-max-scale.png'.format(column)) save_fig(output_file) plt.close()
def createConvexPath(self, pair): #pr = cProfile.Profile() #pr2 = cProfile.Profile() #print pair[1] odPointsList = ((pair[0][0].x, pair[0][0].y), (pair[0][1].x, pair[0][1].y)) #st_line = LineString(odPointsList) labeledObstaclePoly = [] totalConvexPathList = {} dealtArcList = {} totalConvexPathList[odPointsList] = LineString(odPointsList) terminate = 0 idx_loop1 = 0 #sp_l_set = [] time_loop1 = 0 time_contain2 = 0 time_crossingDict = 0 time_convexLoop = 0 time_impedingArcs = 0 time_spatialFiltering = 0 time_loop1_crossingDict = 0 time_buildConvexHulls = 0 while terminate == 0: t1s = time.time() idx_loop1 += 1 t6s = time.time() #w = shapefile.Writer(shapefile.POLYLINE) #w.field('nem') #for line in totalConvexPathList: #w.line(parts=[[ list(x) for x in line ]]) #w.record('ff') #w.save(self.path + "graph_" + str(idx_loop1) + self.version_name) totalGrpah = self.createGraph(totalConvexPathList.keys()) spatial_filter_n = networkx.dijkstra_path(totalGrpah, odPointsList[0], odPointsList[1]) spatial_filter = [] for i in xrange(len(spatial_filter_n) - 1): spatial_filter.append( [spatial_filter_n[i], spatial_filter_n[i + 1]]) #w = shapefile.Writer(shapefile.POLYLINE) #w.field('nem') #for line in spatial_filter: #w.line(parts=[[ list(x) for x in line ]]) #w.record('ff') #w.save(self.path + "spatial Filter_" + str(idx_loop1) + self.version_name) #sp_length = 0 #for j in spatial_filter: #sp_length += LineString(j).length #sp_l_set.append(sp_length) crossingDict = defaultdict(list) for line in spatial_filter: Line = LineString(line) for obs in self.obstaclesPolygons: if Line.crosses(obs): if obs not in labeledObstaclePoly: labeledObstaclePoly.append(obs) crossingDict[tuple(line)].append(obs) t6e = time.time() time_spatialFiltering += t6e - t6s if len(crossingDict.keys()) == 0: terminate = 1 continue else: t7s = time.time() for tLine in crossingDict.keys(): #cLine = list(tLine) if dealtArcList.has_key(tLine): try: del totalConvexPathList[tLine] except: del totalConvexPathList[(tLine[1], tLine[0])] continue else: dealtArcList[tLine] = LineString(list(tLine)) try: del totalConvexPathList[tLine] except: del totalConvexPathList[(tLine[1], tLine[0])] containingObs = [] for obs in crossingDict[tLine]: convexHull = self.createConvexhull(obs, tLine) self.splitBoundary(totalConvexPathList, convexHull) convexHull = self.createConvexhull( obs, odPointsList) self.splitBoundary(totalConvexPathList, convexHull) convexHull2 = self.createConvexhull(obs) if convexHull2.contains(Point(tLine[0])): containingObs.append(obs) elif convexHull2.contains(Point(tLine[1])): containingObs.append(obs) if len(containingObs) != 0: #SPLIT subConvexPathList = {} vi_obs = MultiPolygon([x for x in containingObs]) containedLineCoords = list(tLine) fromX = containedLineCoords[0][0] fromY = containedLineCoords[0][1] toX = containedLineCoords[1][0] toY = containedLineCoords[1][1] fxA = (fromY - toY) / (fromX - toX) fxB = fromY - (fxA * fromX) minX = vi_obs.bounds[0] maxX = vi_obs.bounds[2] split_line = LineString([ (min(minX, fromX, toX), fxA * min(minX, fromX, toX) + fxB), (max(maxX, fromX, toX), fxA * max(maxX, fromX, toX) + fxB) ]) for obs in containingObs: s1, s2 = self.splitPolygon(split_line, obs) dividedObsPoly = [] #to deal with multipolygon a = s1.intersection(obs) b = s2.intersection(obs) if a.type == "Polygon": dividedObsPoly.append(a) else: for o in a.geoms: if o.type == "Polygon": dividedObsPoly.append(o) if b.type == "Polygon": dividedObsPoly.append(b) else: for o2 in b.geoms: if o2.type == "Polygon": dividedObsPoly.append(o2) for obs2 in dividedObsPoly: for pt in tLine: convexHull = self.createConvexhull( obs2, [pt]) self.splitBoundary( subConvexPathList, convexHull) subVertices = [] for line in subConvexPathList: subVertices.extend(line) subVertices = list(set(subVertices)) containingObsVertices = [] for obs in containingObs: containingObsVertices.extend( list(obs.exterior.coords)) subVertices = [ x for x in subVertices if x in containingObsVertices ] deleteList = [] for line in subConvexPathList: chk_cross = 0 for obs in containingObs: if subConvexPathList[line].crosses(obs): chk_cross = 1 if chk_cross == 1: deleteList.append(line) for line in deleteList: del subConvexPathList[line] #subConvexPathList.remove(line) pairList = [] for i in range(len(subVertices)): for j in range(i + 1, len(subVertices)): pairList.append( (subVertices[i], subVertices[j])) for i in pairList: Line = LineString(i) chk_cross = 0 for obs in containingObs: if Line.crosses(obs): chk_cross = 1 elif Line.within(obs): chk_cross = 1 if chk_cross == 0: subConvexPathList[i] = Line #subConvexPathList.append(i) buffer_st_line = split_line.buffer(0.1) deleteList = [] for line in subConvexPathList: if buffer_st_line.contains( subConvexPathList[line]): deleteList.append(line) for line in deleteList: if subConvexPathList.has_key(line): del subConvexPathList[line] #subConvexPathList = [x for x in subConvexPathList if x not in deleteList] for line in subConvexPathList: if not totalConvexPathList.has_key(line): if not totalConvexPathList.has_key( (line[1], line[0])): totalConvexPathList[ line] = subConvexPathList[ line] #if line not in totalConvexPathList: #if [line[1], line[0]] not in totalConvexPathList: #totalConvexPathList.append(line) #w = shapefile.Writer(shapefile.POLYLINE) #w.field('nem') #for line in totalConvexPathList: #w.line(parts=[[ list(x) for x in line ]]) #w.record('ff') #w.save(self.path + "graph2_" + str(idx_loop1) + self.version_name) t7e = time.time() time_loop1_crossingDict += t7e - t7s #new lines labeled_multyPoly = MultiPolygon( [x for x in labeledObstaclePoly]) convexHull = self.createConvexhull(labeled_multyPoly, odPointsList) self.splitBoundary(totalConvexPathList, convexHull) #new lines end #impededPathList t5s = time.time() impededPathList = {} for line in totalConvexPathList: for obs in labeledObstaclePoly: if totalConvexPathList[line].crosses(obs): impededPathList[line] = totalConvexPathList[line] break t5e = time.time() time_impedingArcs += t5e - t5s for line in impededPathList: del totalConvexPathList[line] terminate2 = 0 idx_loop2 = 0 t1e = time.time() time_loop1 += t1e - t1s #w = shapefile.Writer(shapefile.POLYGON) #w.field('net') #for obs in labeledObstaclePoly: #w.poly(parts=[[list(x) for x in list(obs.exterior.coords)]]) #w.record('ff') #w.save(self.path + "obs"+ str(idx_loop1) + "_" + self.version_name) while terminate2 == 0: idx_loop2 += 1 deleteList = [] crossingDict = defaultdict(list) for line in dealtArcList: if impededPathList.has_key(line): del impededPathList[line] elif impededPathList.has_key((line[1], line[0])): del impededPathList[(line[1], line[0])] t3s = time.time() #pr.enable() for line in impededPathList: for obs in labeledObstaclePoly: if impededPathList[line].crosses(obs): crossingDict[line].append(obs) t3e = time.time() time_crossingDict += t3e - t3s #at this point, impededArcList should be emptied, as it only contains crossing arcs, and all of them #should be replaced by convex hulls. for line in crossingDict: del impededPathList[line] for line in impededPathList: if not totalConvexPathList.has_key(line): totalConvexPathList[line] = impededPathList[line] impededPathList = {} if len(crossingDict.keys()) == 0: terminate2 = 1 continue else: #w = shapefile.Writer(shapefile.POLYLINE) #w.field('nem') #for line in crossingDict: #w.line(parts=[[ list(x) for x in line ]]) #w.record('ff') #w.save(self.path + "crossingDict_" + str(idx_loop1) + "_"+ str(idx_loop2) +"_"+ self.version_name) t4s = time.time() for tLine in crossingDict.keys(): dealtArcList[tLine] = crossingDict[tLine] containingObs = [] for obs in crossingDict[tLine]: chk_contain = 0 convexHull2 = self.createConvexhull(obs) if convexHull2.contains(Point(tLine[0])): containingObs.append(obs) chk_contain = 1 elif convexHull2.contains(Point(tLine[1])): containingObs.append(obs) chk_contain = 1 if chk_contain == 0: t10s = time.time() convexHull = self.createConvexhull( obs, tLine) self.splitBoundary(impededPathList, convexHull) t10e = time.time() time_buildConvexHulls += t10e - t10s if len(containingObs) != 0: #SPLIT #print "SPLIT" t2s = time.time() subConvexPathList = {} vi_obs = MultiPolygon( [x for x in containingObs]) containedLineCoords = tLine fromX = containedLineCoords[0][0] fromY = containedLineCoords[0][1] toX = containedLineCoords[1][0] toY = containedLineCoords[1][1] fxA = (fromY - toY) / (fromX - toX) fxB = fromY - (fxA * fromX) minX = vi_obs.bounds[0] maxX = vi_obs.bounds[2] split_line = LineString([ (min(minX, fromX, toX), fxA * min(minX, fromX, toX) + fxB), (max(maxX, fromX, toX), fxA * max(maxX, fromX, toX) + fxB) ]) for obs in containingObs: s1, s2 = self.splitPolygon(split_line, obs) dividedObsPoly = [] #to deal with multipolygon a = s1.intersection(obs) b = s2.intersection(obs) if a.type == "Polygon": dividedObsPoly.append(a) else: for o in a.geoms: if o.type == "Polygon": dividedObsPoly.append(o) if b.type == "Polygon": dividedObsPoly.append(b) else: for o2 in b.geoms: if o2.type == "Polygon": dividedObsPoly.append(o2) for obs2 in dividedObsPoly: for pt in tLine: convexHull = self.createConvexhull( obs2, [pt]) self.splitBoundary( subConvexPathList, convexHull) subVertices = [] for line in subConvexPathList: subVertices.extend(line) subVertices = list(set(subVertices)) containingObsVertices = [] for obs in containingObs: containingObsVertices.extend( list(obs.exterior.coords)) subVertices = [ x for x in subVertices if x in containingObsVertices ] deleteList = [] for line in subConvexPathList: chk_cross = 0 for obs in containingObs: if subConvexPathList[line].crosses( obs): chk_cross = 1 if chk_cross == 1: deleteList.append(line) for line in deleteList: del subConvexPathList[line] pairList = [] for i in range(len(subVertices)): for j in range(i + 1, len(subVertices)): pairList.append( (subVertices[i], subVertices[j])) for i in pairList: Line = LineString(list(i)) chk_cross = 0 for obs in containingObs: if Line.crosses(obs): chk_cross = 1 elif Line.within(obs): chk_cross = 1 if chk_cross == 0: subConvexPathList[i] = Line buffer_st_line = split_line.buffer(0.1) deleteList = [] for line in subConvexPathList: if buffer_st_line.contains( subConvexPathList[line]): deleteList.append(line) for line in deleteList: del subConvexPathList[line] for line in subConvexPathList: if not impededPathList.has_key(line): if not impededPathList.has_key( (line[1], line[0])): impededPathList[ line] = subConvexPathList[line] t2e = time.time() time_contain2 += t2e - t2s #pr.disable() for line in dealtArcList: if impededPathList.has_key(line): del impededPathList[line] #impededPathList = [x for x in impededPathList if x not in dealtArcList] t4e = time.time() time_convexLoop += t4e - t4s #end of else #w = shapefile.Writer(shapefile.POLYLINE) #w.field('nem') #for line in impededPathList: #w.line(parts=[[ list(x) for x in line ]]) #w.record('ff') #w.save(self.path + "After_graph_" + str(idx_loop1) + "_"+ str(idx_loop2) +"_"+ self.version_name) #end of while2 for line in impededPathList: if not totalConvexPathList.has_key(line): totalConvexPathList[line] = impededPathList[line] #totalConvexPathList.extend(impededPathList) totalGraph = self.createGraph(totalConvexPathList.keys()) esp_n = networkx.dijkstra_path(totalGraph, odPointsList[0], odPointsList[1]) esp = [] for i in esp_n: esp.append(i) #print esp esp_shp = LineString(esp) return esp_shp
from shapely import speedups; assert speedups.available; speedups.enable() from shapely.geometry import LineString ls = LineString([(0, 0), (10, 0)]) # On OSX causes an abort trap, due to https://github.com/Toblerity/Shapely/issues/177. r = ls.wkt area = ls.buffer(10).area
def rrt(bounds, environment, start_pose, radius, end_region): ''' - bounds: (minx, miny, maxx, maxy) tuple over region - environment: instance of the Environment class that describes the placement of the obstacles in the scene - start_pose: start_pose = (x,y) is a tuple indicating the starting position of the robot - radius: radius is the radius of the robot (used for collision checking) - end_region: end_region is a shapely Polygon that describes the region that the robot needs to reach ''' # Adding tuples to nodes -> represent all nodes expanded by tree nodes = [start_pose] # Creating graph of SearchNodes from the tuples to represent the tree with parents. Used to order and graph = Graph() graph.add_node(SearchNode(start_pose)) goalPath = Path( SearchNode(start_pose)) # for initialization in case of no path found # Draw the environment (with its obstacles) and with the start node + end region if plotting: ax = plot_environment(environment, bounds) plot_poly(ax, Point(start_pose).buffer(radius, resolution=3), 'blue') plot_poly(ax, end_region, 'red', alpha=0.2) for i in range(10000): # random high number # this range must be evaluated according to size of problem. RRT do not ensure any solution paths # sample a random node inside bounds (random number between xmin/max and ymin/max). all bound-"checking" is donw here: makes sure we don't have to do ant checks later rand_xval = random.uniform(bounds[0], bounds[2]) rand_yval = random.uniform(bounds[1], bounds[3]) node_rand = (rand_xval, rand_yval) ''' --- For every x'th iteration - aim towards goal. A good choice varies, and depends on the step length that I decided in steerpath(). Not having this makes total number of nodes blow up. I have kept choices of when to goal bias and the travel distance in steering function to perform well in large environments. TODO: This number depends on complexity of space. Easy space: lower number --- Then, find out which node in our list that is the nearest to the random node. Returns a searchNode and a float distance. --- Steer towards the new node -> correct parents and add cost --- Check if the new steered node is in bounds. --- If not visited before (avoid cycle) and no obstacles are in the path: add to tree ''' sampling_rate = 12 if not (i % sampling_rate): node_rand = end_region.centroid.coords[0] node_nearest, node_dist = nearestSNode(graph, node_rand) ## TODO: this steering function should somehow consider dynamics steered_node = steerPath(node_nearest.state, node_rand) if not (bounds[0] < steered_node[0] < bounds[2]) or not (bounds[1] < steered_node[1] < bounds[3]): continue # sometimes not checking for this made the path go out of bounds node_steered = SearchNode(steered_node, node_nearest, node_nearest.cost + node_dist) if node_steered.state not in nodes: if not obstacleIsInPath(node_nearest.state, node_steered.state, environment, radius): # Keep track of total number of nodes in tree # Add edge (also adds new nodes to graph) and weight # Plot non-colliding edge to show all searched nodes nodes.append(node_steered.state) graph.add_edge(node_nearest, node_steered, node_dist) if plotting: line = LineString([node_nearest.state, node_steered.state]) plot_line_mine(ax, line) else: continue # Avoid goal check if collision is found # Check last addition for goal state # TODO: ADDED EXTRA SAFETY IN GOAL CHECK if goalReached(node_steered.state, 1.5 * radius, end_region): goalPath = Path(node_steered) break # break the while loop when solution is found! # No. of nodes in total - No. of nodes in sol path - Sol path length noOfTotalNodes = len(nodes) noOfNodesInSol = len(goalPath.path) pathLength = goalPath.cost if plotting: for i in range(noOfNodesInSol - 1): # Draw goal path line = LineString([goalPath.path[i], goalPath.path[i + 1]]) plot_line_mine(ax, line) # Draw the expanded line expanded_line = line.buffer(radius, resolution=3) plot_poly(ax, expanded_line, 'green', alpha=0.2) # plotting last node in goalPath and setting title to format in task plot_poly(ax, Point(goalPath.path[-1]).buffer(radius, resolution=3), 'blue') titleString = "Nodes total / in solution path: %s/ %s \nPath length: %0.3f" ax.set_title(titleString % (noOfTotalNodes, noOfNodesInSol, pathLength)) return goalPath.path
class B_Creature: """ Generates a complete virtual creature Tests ----------- """ def __init__(self, params): """ Initialises a simple L-system Parameters ---------- variables : str a string containing all of the letters that take part in the recursion. These letters should also have associated rules. constants : str or None a string containing all the letters that do not take part in the recursion. These letters will not have an associated rule axiom : str The initial character string rules : dict a dictionary containing the rules for recursion. This is a dictionary of listing the letter replacement in the recursion. eg. {"A": "AB", "B": "A"} """ self.choices = [] self.rules = params.get('rules') self.l_string = params.get('axiom') self.constants = params.get('constants') self.variables = params.get('variables') self.angle = radians(params.get('angle')) self.recur(params.get('recurs')) self.length = params.get('length') self.mapper() self.tolines() self.layout() self.results() # def recur(self, n): # for _ in range(n): # self.l_string = ''.join([self.next_char(c) for c in self.l_string]) # self.l_string = self.l_string.replace('X', '') def recur(self, iters): for _ in range(iters + 1): if self.l_string.count('X') == 0: break else: self.l_string = ''.join( [self.next_char(c) for c in self.l_string]) def next_char(self, c): rule = self.rules.get(c, c) if not rule == c: key, choice = random.choice(list(self.rules.get(c).items())) self.choices.append(key) return choice else: return rule # rule = self.rules.get(c, c) # if len(rule) > 1: # return np.random.choice(self.rules.get(c, ["Other"])["options"], # p=self.rules.get(c, ["Other"])[ # "probabilities"]) # else: # return rule def mapper(self): """Converts L-string to coordinates Returns ------- List List of coordinates """ num_chars = len(self.l_string) coords = np.zeros((num_chars + 1, 4), np.double) nodes = np.zeros((1, 4), np.double) rotVec = np.array(((cos(self.angle), -sin(self.angle), 0), (sin(self.angle), cos(self.angle), 0), (0, 0, 1))) start_vec = np.array((0, 1, 0), np.float64) curr_vec = start_vec i = 1 for c in self.l_string: if c == 'F': coords[i, :3] = (coords[i - 1, :3] + (self.length * curr_vec)) i += 1 if c == '-': curr_vec = np.dot(curr_vec, (-1 * rotVec)) if c == '+': curr_vec = np.dot(curr_vec, rotVec) if c == '[': nodes = np.vstack((nodes, coords[i - 1])) coords[i - 1, 3] = 3 nodes[-1, 3] = 1 if c == ']': if coords[i - 1, 3] == 1: coords[i, 3] = 2 coords[i - 1] = nodes[-1] i += 1 else: coords[i - 1, 3] = 2 coords[i] = nodes[-1] i += 1 coords = np.delete(coords, np.s_[i:], 0) self.coords = coords def tolines(self): """Converts L-string coordinates to individual line segments Returns ------- List List of L-string lines """ lines = [] j = 0 for i in range(len(self.coords)): if (self.coords[i, 3] == 2) or (i == (len(self.coords) - 1)): lines.append(self.coords[j:i + 1, :2].tolist()) j = i + 1 if not lines: self.lines = [self.coords[:, :3]] else: self.lines = [line for line in lines if len(line) > 1] def layout(self): """Converts coordinates or lines to shapely object and buffers Returns ------- Shapely object L-creature "body" """ if len(self.lines) > 1: self.linestring = MultiLineString(self.lines) else: self.linestring = LineString(self.coords[:, :2]) self.Area = self.linestring.buffer(self.length / 2).area self.Bounds = self.linestring.bounds def results(self): chars = set(list(self.constants + self.variables)) avgs = dict() for i in chars: avgs[i] = re.findall( '(?<=' + re.escape(i) + ').*?(?=' + re.escape(i) + ')', self.l_string) for i in chars: if len(avgs[i]) == 0: avgs[i] = 0.0 else: avgs[i] = sum([len(i) for i in avgs[i]]) / len(avgs[i]) try: maxF = max(len(s) for s in re.findall(r'[F]+', self.l_string)) except: maxF = 0 try: maxP = max(len(s) for s in re.findall(r'[+]+', self.l_string)) except: maxP = 0 try: maxM = max(len(s) for s in re.findall(r'[-]+', self.l_string)) except: maxM = 0 self.perF = self.l_string.count('F') / len(self.l_string) self.F = self.l_string.count('F') self.perP = self.l_string.count('+') / len(self.l_string) self.perM = self.l_string.count('-') / len(self.l_string) self.perX = self.l_string.count('X') / len(self.l_string) if '[' in self.constants: self.perB = self.l_string.count('[') / len(self.l_string) self.perN = self.l_string.count(']') / len(self.l_string) self.maxF = maxF self.maxP = maxP self.maxM = maxM self.avgF = avgs.get('F') self.avgP = avgs.get('+') self.avgM = avgs.get('-') self.comp = np.linalg.norm(self.bounds) self.fitness = self.area / self.comp self.ratio = np.array([ (self.choices.count(1) / len(self.choices)) * self.fitness, (self.choices.count(2) / len(self.choices)) * self.fitness, ]) self.rules = list(self.rules['X'].values())
for y in range(0,world_map.shape[1]): if world_map[y][x] == 0.0: tuple_list.append((y,x)) d=0.2 polygons = [Point(x,y).buffer(0.5,16,CAP_STYLE.square,JOIN_STYLE.bevel) for (y,x) in tuple_list] #polygons = [box(x-d,y-d,x+d,y+d) for (y,x) in tuple_list] fig = pyplot.figure(1,figsize = [14,7],dpi = 90) ax = fig.add_subplot(111) d1 = cascaded_union(polygons) a = LineString([(0,0),(10,10)]) p = PolygonPatch(a.buffer(0.1), fc='#999999', ec='#999999', alpha=1, zorder=2) ax.add_patch(p) for ob in d1: p = PolygonPatch(ob, fc='#999999', ec='#999999', alpha=0.5, zorder=1) ax.add_patch(p) print a.intersects(ob) xrange = [0, 50] yrange = [0, 50] ax.set_xlim(*xrange) ax.set_ylim(*yrange) ax.set_aspect(1)
from figures import SIZE, BLUE, GRAY def plot_line(ax, ob): x, y = ob.xy ax.plot(x, y, color=GRAY, linewidth=3, solid_capstyle='round', zorder=1) line = LineString([(0, 0), (1, 1), (0, 2), (2, 2), (3, 1), (1, 0)]) fig = pyplot.figure(1, figsize=SIZE, dpi=90) # 1 ax = fig.add_subplot(121) plot_line(ax, line) dilated = line.buffer(0.5) patch1 = PolygonPatch(dilated, fc=BLUE, ec=BLUE, alpha=0.5, zorder=2) ax.add_patch(patch1) ax.set_title('a) dilation') xrange = [-1, 4] yrange = [-1, 3] ax.set_xlim(*xrange) ax.set_xticks(range(*xrange) + [xrange[-1]]) ax.set_ylim(*yrange) ax.set_yticks(range(*yrange) + [yrange[-1]]) ax.set_aspect(1) #2 ax = fig.add_subplot(122)
from matplotlib import pyplot from shapely.geometry import LineString from descartes import PolygonPatch from figures import SIZE, BLUE, GRAY, set_limits, plot_line line = LineString([(0, 0), (1, 1), (0, 2), (2, 2), (3, 1), (1, 0)]) fig = pyplot.figure(1, figsize=SIZE, dpi=90) # 1 ax = fig.add_subplot(121) plot_line(ax, line) dilated = line.buffer(0.5, cap_style=3) patch1 = PolygonPatch(dilated, fc=BLUE, ec=BLUE, alpha=0.5, zorder=2) ax.add_patch(patch1) ax.set_title('a) dilation, cap_style=3') set_limits(ax, -1, 4, -1, 3) #2 ax = fig.add_subplot(122) patch2a = PolygonPatch(dilated, fc=GRAY, ec=GRAY, alpha=0.5, zorder=1) ax.add_patch(patch2a) eroded = dilated.buffer(-0.3)
def _check(self, aerodrome_type, runway_kind_detail): import dsl from tilequeue.tile import coord_to_bounds from shapely.geometry import LineString from shapely.geometry import CAP_STYLE from ModestMaps.Core import Coordinate z, x, y = (16, 0, 0) bounds = coord_to_bounds(Coordinate(zoom=z, column=x, row=y)) # runway line that runs from a quarter to three quarters of the # tile diagonal. this is so that we can buffer it without it # going outside the tile boundary. runway_line = LineString([ [bounds[0] + 0.25 * (bounds[2] - bounds[0]), bounds[1] + 0.25 * (bounds[3] - bounds[1])], [bounds[0] + 0.75 * (bounds[2] - bounds[0]), bounds[1] + 0.75 * (bounds[3] - bounds[1])], ]) # runway polygon which has the runway line as a centreline. runway_poly = runway_line.buffer( 0.1 * (bounds[2] - bounds[0]), # width 1/10th of a tile cap_style=CAP_STYLE.flat, ) self.generate_fixtures( dsl.way(1, dsl.tile_box(z, x, y), { 'aeroway': 'aerodrome', 'aerodrome:type': aerodrome_type, 'name': 'Fake Aerodrome', 'source': 'openstreetmap.org', }), # runway line dsl.way(2, runway_line, { 'aeroway': 'runway', 'source': 'openstreetmap.org', }), # runway polygon dsl.way(3, runway_poly, { 'area:aeroway': 'runway', 'source': 'openstreetmap.org', }) ) # runway line ends up in roads layer self.assert_has_feature( z, x, y, 'roads', { 'id': 2, 'kind': 'aeroway', 'kind_detail': 'runway', 'aerodrome_kind_detail': runway_kind_detail, }) # runway polygon is in landuse self.assert_has_feature( z, x, y, 'landuse', { 'id': 3, 'kind': 'runway', 'kind_detail': runway_kind_detail, })
from computePrimitives import * import matplotlib.pyplot as plt from shapely.geometry import LineString, Point from descartes import PolygonPatch import pdb motion_primitives = computePrimitives() fig = plt.figure(2) fig.clear() plt.ion() ax = fig.add_subplot(111, aspect = 'equal') for mp in motion_primitives[0.0]: color = '#9999FF' if mp.isbackward: color = '#99FF99' a = LineString(mp.path) p = PolygonPatch(a.buffer(0.1), fc=color, ec=color, alpha=1, zorder=2) ax.add_patch(p) p2 = PolygonPatch(Point(mp.delta_state[0:2]).buffer(0.1), fc='000000', ec='000000', alpha=1, zorder=3) ax.add_patch(p2) ax.set_xlim(-3.5,3.5) ax.set_ylim(-3.5,3.5) ax.grid() fig.show() plt.pause(0.1) pdb.set_trace()
paths = toolpath.generate(shape.figs) for tpl in paths: print type(tpl) plot_line(ax, tpl, color=GREEN, width=10) toolpath = HoldersToolpath(holder_size=2, min_len=10) toolpath.add_tool(2) paths = toolpath.generate(shape.figs) for tpl in paths: print type(tpl) #plot_line(ax, tpl, color=RED) brd = LineString(tpl) b = brd.buffer(2/2, cap_style=1, join_style=1, resolution=8) plot_line(ax, b.exterior, color=BLUE) #~ obj1 = shape.figs[0] #~ obj = obj1 #~ #~ plot_line(ax, obj, color=RED) #~ #~ opt_obj = obj.simplify(tolerance=0.01, preserve_topology=False) #~ #~ print opt_obj.xy #~ #~ plot_line(ax, obj, color=GREEN) #~ #~ min_len = 10 #~ holder_length = 3
expCircle = np.array(expCircle.exterior.coords) # horseshoe tumor rad=.007 loc=[0.0229845803642/2.0-.002,.02] circle = Point(loc[0],loc[1]).buffer(rad) circle = np.array(circle.exterior.coords) semicircle=circle[circle[:,0]>=loc[0]] semi=semicircle[semicircle[:,1].argsort()] line = LineString(semi)#LineString([(0, 0), (1, 1), (0, 2), (2, 2), (3, 1), (1, 0)]) horseshoe = LineString(semi).buffer(.0025) horseshoe = np.array(horseshoe.exterior.coords) # additional tumor othertumor = LineString(.008*np.array([(0, 0), (1, 1), (0, 2), (2, 2), (3, 1), (1, 0)])) othertumor = othertumor.buffer(.005) othertumor = np.array(othertumor.exterior.coords) ####################################### # Functions for simulating deflection measurements ####################################### def sigmoid(dist, alpha=1400, a=0.0, b=1.0): """ a, b: base and max readings of the probe with and without tumor dist = xProbe-xEdge xProbe: xEdge: the center of sigmoid alpha: slope Output:
def createConvexPath_FD(pair): #For F_D pair only #return demand_id if ESP distance to target demand is less than fd_delivery print pair[1] odPointsList = ((pair[0][0].x, pair[0][0].y), (pair[0][1].x, pair[0][1].y)) st_line = LineString(odPointsList) labeledObstaclePoly = [] totalConvexPathList = {} if st_line.length > fd_delivery * 5280: return 0 dealtArcList = {} totalConvexPathList[odPointsList] = LineString(odPointsList) LineString terminate = 0 idx_loop1 = 0 time_loop1 = 0 time_contain2 = 0 time_crossingDict = 0 time_convexLoop = 0 time_impedingArcs = 0 time_spatialFiltering = 0 time_loop1_crossingDict = 0 time_buildConvexHulls = 0 no_obs = False while terminate == 0: t1s = time.time() idx_loop1 += 1 t6s = time.time() #w = shapefile.Writer(shapefile.POLYLINE) #w.field('nem') #for line in totalConvexPathList: #w.line(parts=[[ list(x) for x in line ]]) #w.record('ff') #w.save(path + "graph_" + str(idx_loop1) + version_name) totalGrpah = createGraph(totalConvexPathList.keys()) spatial_filter_n = networkx.dijkstra_path(totalGrpah, odPointsList[0], odPointsList[1]) spatial_filter = [] for i in xrange(len(spatial_filter_n)-1): spatial_filter.append([spatial_filter_n[i], spatial_filter_n[i+1]]) #w = shapefile.Writer(shapefile.POLYLINE) #w.field('nem') #for line in spatial_filter: #w.line(parts=[[ list(x) for x in line ]]) #w.record('ff') #w.save(self.path + "spatial Filter_" + str(idx_loop1) + self.version_name) #sp_length = 0 #for j in spatial_filter: #sp_length += LineString(j).length #sp_l_set.append(sp_length) crossingDict = defaultdict(list) for line in spatial_filter: Line = LineString(line) for obs in obstaclesPolygons: if Line.crosses(obs): if obs not in labeledObstaclePoly: labeledObstaclePoly.append(obs) crossingDict[tuple(line)].append(obs) t6e = time.time() time_spatialFiltering += t6e - t6s if len(crossingDict.keys()) == 0: terminate = 1 no_obs = True continue else: t7s = time.time() for tLine in crossingDict.keys(): #cLine = list(tLine) if dealtArcList.has_key(tLine): try: del totalConvexPathList[tLine] except: del totalConvexPathList[(tLine[1], tLine[0])] continue else: dealtArcList[tLine] = LineString(list(tLine)) try: del totalConvexPathList[tLine] except: del totalConvexPathList[(tLine[1], tLine[0])] containingObs = [] for obs in crossingDict[tLine]: convexHull = createConvexhull(obs, tLine) splitBoundary(totalConvexPathList, convexHull) convexHull = createConvexhull(obs, odPointsList) splitBoundary(totalConvexPathList, convexHull) convexHull2 = createConvexhull(obs) if convexHull2.contains(Point(tLine[0])): containingObs.append(obs) elif convexHull2.contains(Point(tLine[1])): containingObs.append(obs) if len(containingObs) != 0: #SPLIT subConvexPathList = {} vi_obs = MultiPolygon([x for x in containingObs]) containedLineCoords = list(tLine) fromX = containedLineCoords[0][0] fromY = containedLineCoords[0][1] toX = containedLineCoords[1][0] toY = containedLineCoords[1][1] fxA = (fromY - toY) / (fromX - toX) fxB = fromY - (fxA * fromX) minX = vi_obs.bounds[0] maxX = vi_obs.bounds[2] split_line = LineString([(min(minX, fromX, toX), fxA * min(minX, fromX, toX) + fxB), (max(maxX, fromX, toX), fxA * max(maxX, fromX, toX) + fxB)]) for obs in containingObs: s1, s2 = splitPolygon(split_line, obs) dividedObsPoly = [] #to deal with multipolygon a = s1.intersection(obs) b = s2.intersection(obs) if a.type == "Polygon": dividedObsPoly.append(a) else: for o in a.geoms: if o.type == "Polygon": dividedObsPoly.append(o) if b.type == "Polygon": dividedObsPoly.append(b) else: for o2 in b.geoms: if o2.type == "Polygon": dividedObsPoly.append(o2) for obs2 in dividedObsPoly: for pt in tLine: convexHull = createConvexhull(obs2, [pt]) splitBoundary(subConvexPathList, convexHull) subVertices = [] for line in subConvexPathList: subVertices.extend(line) subVertices = list(set(subVertices)) containingObsVertices = [] for obs in containingObs: containingObsVertices.extend(list(obs.exterior.coords)) subVertices = [x for x in subVertices if x in containingObsVertices] deleteList = [] for line in subConvexPathList: chk_cross = 0 for obs in containingObs: if subConvexPathList[line].crosses(obs): chk_cross = 1 if chk_cross == 1: deleteList.append(line) for line in deleteList: del subConvexPathList[line] #subConvexPathList.remove(line) pairList = [] for i in range(len(subVertices)): for j in range(i+1, len(subVertices)): pairList.append((subVertices[i], subVertices[j])) for i in pairList: Line = LineString(i) chk_cross = 0 for obs in containingObs: if Line.crosses(obs): chk_cross = 1 elif Line.within(obs): chk_cross = 1 if chk_cross == 0: subConvexPathList[i] = Line #subConvexPathList.append(i) buffer_st_line = split_line.buffer(0.1) deleteList = [] for line in subConvexPathList: if buffer_st_line.contains(subConvexPathList[line]): deleteList.append(line) for line in deleteList: if subConvexPathList.has_key(line): del subConvexPathList[line] #subConvexPathList = [x for x in subConvexPathList if x not in deleteList] for line in subConvexPathList: if not totalConvexPathList.has_key(line): if not totalConvexPathList.has_key((line[1],line[0])): totalConvexPathList[line] = subConvexPathList[line] #if line not in totalConvexPathList: #if [line[1], line[0]] not in totalConvexPathList: #totalConvexPathList.append(line) #w = shapefile.Writer(shapefile.POLYLINE) #w.field('nem') #for line in totalConvexPathList: #w.line(parts=[[ list(x) for x in line ]]) #w.record('ff') #w.save(self.path + "graph2_" + str(idx_loop1) + self.version_name) t7e = time.time() time_loop1_crossingDict += t7e - t7s #new lines labeled_multyPoly = MultiPolygon([x for x in labeledObstaclePoly]) convexHull = createConvexhull(labeled_multyPoly, odPointsList) splitBoundary(totalConvexPathList, convexHull) #new lines end #impededPathList t5s = time.time() impededPathList = {} for line in totalConvexPathList: for obs in labeledObstaclePoly: if totalConvexPathList[line].crosses(obs): impededPathList[line] = totalConvexPathList[line] break t5e = time.time() time_impedingArcs += t5e - t5s for line in impededPathList: del totalConvexPathList[line] terminate2 = 0 idx_loop2 = 0 t1e = time.time() time_loop1 += t1e - t1s while terminate2 == 0: idx_loop2 += 1 deleteList = [] crossingDict = defaultdict(list) for line in dealtArcList: if impededPathList.has_key(line): del impededPathList[line] elif impededPathList.has_key((line[1], line[0])): del impededPathList[(line[1],line[0])] t3s = time.time() #pr.enable() for line in impededPathList: for obs in labeledObstaclePoly: if impededPathList[line].crosses(obs): crossingDict[line].append(obs) t3e = time.time() time_crossingDict += t3e - t3s #at this point, impededArcList should be emptied, as it only contains crossing arcs, and all of them #should be replaced by convex hulls. for line in crossingDict: del impededPathList[line] for line in impededPathList: if not totalConvexPathList.has_key(line): totalConvexPathList[line] = impededPathList[line] impededPathList = {} if len(crossingDict.keys()) == 0: terminate2 = 1 continue else: #w = shapefile.Writer(shapefile.POLYLINE) #w.field('nem') #for line in crossingDict: #w.line(parts=[[ list(x) for x in line ]]) #w.record('ff') #w.save(self.path + "crossingDict_" + str(idx_loop1) + "_"+ str(idx_loop2) +"_"+ self.version_name) t4s = time.time() for tLine in crossingDict.keys(): dealtArcList[tLine] = crossingDict[tLine] containingObs = [] for obs in crossingDict[tLine]: chk_contain = 0 convexHull2 = createConvexhull(obs) if convexHull2.contains(Point(tLine[0])): containingObs.append(obs) chk_contain = 1 elif convexHull2.contains(Point(tLine[1])): containingObs.append(obs) chk_contain = 1 if chk_contain == 0: t10s = time.time() convexHull = createConvexhull(obs, tLine) splitBoundary(impededPathList, convexHull) t10e = time.time() time_buildConvexHulls += t10e - t10s if len(containingObs) != 0: #SPLIT #print "SPLIT" t2s = time.time() subConvexPathList = {} vi_obs = MultiPolygon([x for x in containingObs]) containedLineCoords = tLine fromX = containedLineCoords[0][0] fromY = containedLineCoords[0][1] toX = containedLineCoords[1][0] toY = containedLineCoords[1][1] fxA = (fromY - toY) / (fromX - toX) fxB = fromY - (fxA * fromX) minX = vi_obs.bounds[0] maxX = vi_obs.bounds[2] split_line = LineString([(min(minX, fromX, toX), fxA * min(minX, fromX, toX) + fxB), (max(maxX, fromX, toX), fxA * max(maxX, fromX, toX) + fxB)]) for obs in containingObs: s1, s2 = splitPolygon(split_line, obs) dividedObsPoly = [] #to deal with multipolygon a = s1.intersection(obs) b = s2.intersection(obs) if a.type == "Polygon": dividedObsPoly.append(a) else: for o in a.geoms: if o.type == "Polygon": dividedObsPoly.append(o) if b.type == "Polygon": dividedObsPoly.append(b) else: for o2 in b.geoms: if o2.type == "Polygon": dividedObsPoly.append(o2) for obs2 in dividedObsPoly: for pt in tLine: convexHull = createConvexhull(obs2, [pt]) splitBoundary(subConvexPathList, convexHull) subVertices = [] for line in subConvexPathList: subVertices.extend(line) subVertices = list(set(subVertices)) containingObsVertices = [] for obs in containingObs: containingObsVertices.extend(list(obs.exterior.coords)) subVertices = [x for x in subVertices if x in containingObsVertices] deleteList = [] for line in subConvexPathList: chk_cross = 0 for obs in containingObs: if subConvexPathList[line].crosses(obs): chk_cross = 1 if chk_cross == 1: deleteList.append(line) for line in deleteList: del subConvexPathList[line] pairList = [] for i in range(len(subVertices)): for j in range(i+1, len(subVertices)): pairList.append((subVertices[i], subVertices[j])) for i in pairList: Line = LineString(list(i)) chk_cross = 0 for obs in containingObs: if Line.crosses(obs): chk_cross = 1 elif Line.within(obs): chk_cross = 1 if chk_cross == 0: subConvexPathList[i] = Line buffer_st_line = split_line.buffer(0.1) deleteList = [] for line in subConvexPathList: if buffer_st_line.contains(subConvexPathList[line]): deleteList.append(line) for line in deleteList: del subConvexPathList[line] for line in subConvexPathList: if not impededPathList.has_key(line): if not impededPathList.has_key((line[1], line[0])): impededPathList[line] = subConvexPathList[line] t2e = time.time() time_contain2 += t2e - t2s #pr.disable() for line in dealtArcList: if impededPathList.has_key(line): del impededPathList[line] #impededPathList = [x for x in impededPathList if x not in dealtArcList] t4e = time.time() time_convexLoop += t4e - t4s #end of else #w = shapefile.Writer(shapefile.POLYLINE) #w.field('nem') #for line in impededPathList: #w.line(parts=[[ list(x) for x in line ]]) #w.record('ff') #w.save(path + "After_graph_" + str(idx_loop1) + "_"+ str(idx_loop2) +"_"+ version_name) #end of while2 for line in impededPathList: if not totalConvexPathList.has_key(line): totalConvexPathList[line] = impededPathList[line] #totalConvexPathList.extend(impededPathList) #no obstruction if no_obs == True: return 1 totalGraph = createGraph(totalConvexPathList.keys()) esp_n = networkx.dijkstra_path(totalGraph, odPointsList[0], odPointsList[1]) esp = [] for i in range(len(esp_n)-1): esp.append([esp_n[i], esp_n[i+1]]) #w = shapefile.Writer(shapefile.POLYLINE) #w.field('nem') #no_edges = 0 #for line in totalConvexPathList.keys(): #no_edges += 1 #w.line(parts=[[ list(x) for x in line ]]) #w.record('ff') #w.save(path + "totalpath" + version_name + "%d" % pair[1] ) w = shapefile.Writer(shapefile.POLYLINE) w.field('nem') for line in esp: w.line(parts=[[ list(x) for x in line ]]) w.record('ff') w.save(path + "ESP_" + version_name + "%d" % pair[1]) targetPysal = pysal.IOHandlers.pyShpIO.shp_file(path + "ESP_" + version_name + "%d" % pair[1]) targetShp = generateGeometry(targetPysal) total_length = 0 for line in targetShp: total_length += line.length if total_length <= fd_delivery: return 1 else: return 0
def test_intersects(): circle = Point([1,2]).buffer(2) line = LineString([[0,0],[2,0]]) c = line.buffer(1) circle.intersects(c)
class Vermiculus(object): """ Makes a worm like creature """ def __init__( self, instance_number, starting_point=np.array([0, 0]), starting_vector=np.array([0, 1]), dna_length=60, unit_length=1.0, unit_width=1.0, ): """ Initialise a worm Parameters ---------- starting_point : array like a 2d vector containing the strarting point for the worm instance_number: int the index for the instance of the worm """ self.instance_number = instance_number self.starting_point = starting_point self.starting_vector = starting_vector self.dna_length = dna_length self.unit_length = unit_length self.unit_width = unit_width self.proteins = {"F": 0.0, "L": 45, "R": -45} self.amino_acids = list(self.proteins.keys()) self.amino_probabilities = [0.4, 0.3, 0.3] self.phenotype = [self.starting_point] self.dna = self.transcribe_dna() self.topology = None self.topology_dilated = None self.area = None def transcribe_dna(self): """ Build a DNA string for the worm that is N elements long by selecting from the available amino acids Returns ------- A string N elements long """ return ''.join( np.random.choice(self.amino_acids, p=self.amino_probabilities) for _ in range(self.dna_length)) def _translate_dna(self): """ read the DNA sequence and translate it into the a series of segments. Returns ------- """ cv = self.starting_vector cp = self.starting_point line_cords = [cp] for acid in self.dna: cp, cv = next_point(self.proteins[acid], cv, self.unit_length, cp) line_cords.append(cp) self.phenotype = line_cords def build_topology(self): """ Translate the phenotype to the topology Returns ------- """ self._translate_dna() self.topology = LineString(self.phenotype) self.topology_dilated = self.topology.buffer(self.unit_width) self.area = self.topology_dilated.area
from matplotlib import pyplot from shapely.geometry import LineString from descartes import PolygonPatch from figures import SIZE, BLUE, GRAY, set_limits, plot_line line = LineString([(0, 0), (1, 1), (0, 2), (2, 2), (3, 1), (1, 0)]) fig = pyplot.figure(1, figsize=SIZE, dpi=90) # 1 ax = fig.add_subplot(121) plot_line(ax, line) left_hand_side = line.buffer(0.5, single_sided=True) patch1 = PolygonPatch(left_hand_side, fc=BLUE, ec=BLUE, alpha=0.5, zorder=2) ax.add_patch(patch1) ax.set_title('a) left hand buffer') set_limits(ax, -1, 4, -1, 3) #2 ax = fig.add_subplot(122) plot_line(ax, line) right_hand_side = line.buffer(-0.3, single_sided=True) patch2 = PolygonPatch(right_hand_side, fc=GRAY, ec=GRAY, alpha=0.5, zorder=1) ax.add_patch(patch2)
def main(): config = load_config() coastal_edge_file = os.path.join(config['paths']['data'], 'Results', 'Failure_shapefiles', 'weighted_edges_failures_national_water_transfer_from_road_10_shift.shp') color = '#045a8d' color_by_type = {'Coastal Line': color} columns = ['min_tons', 'max_tons'] column_label_divisors = {c: 1 for c in columns} legend_label = "AADF (tons/day)" title_cols = ['Total tonnage (min)','Total tonnage (max)'] for c in range(len(columns)): ax = get_axes() plot_basemap(ax, config['paths']['data'],highlight_region = []) scale_bar(ax, location=(0.8, 0.05)) plot_basemap_labels(ax, config['paths']['data']) proj_lat_lon = ccrs.PlateCarree() column = columns[c] weights = [ record.attributes[column] for record in shpreader.Reader(coastal_edge_file).records() ] max_weight = max(weights) width_by_range = generate_weight_bins(weights) geoms_by_range = {} for value_range in width_by_range: geoms_by_range[value_range] = [] for record in shpreader.Reader(coastal_edge_file).records(): val = record.attributes[column] geom = record.geometry if val > 0: #only add edges that carry this commodity for nmin, nmax in geoms_by_range: if nmin <= val and val < nmax: geoms_by_range[(nmin, nmax)].append(geom) # plot for range_, width in width_by_range.items(): ax.add_geometries( [geom.buffer(width) for geom in geoms_by_range[range_]], crs=proj_lat_lon, edgecolor='none', facecolor=color, zorder=2) x_l = 102.3 x_r = x_l + 0.4 base_y = 14 y_step = 0.4 y_text_nudge = 0.1 x_text_nudge = 0.1 ax.text( x_l, base_y + y_step - y_text_nudge, legend_label, horizontalalignment='left', transform=proj_lat_lon, size=10) divisor = column_label_divisors[column] for (i, ((nmin, nmax), width)) in enumerate(width_by_range.items()): y = base_y - (i*y_step) line = LineString([(x_l, y), (x_r, y)]) ax.add_geometries( [line.buffer(width)], crs=proj_lat_lon, linewidth=0, edgecolor=color, facecolor=color, zorder=2) if nmin == max_weight: label = '>{:.2f}'.format(max_weight/divisor) else: label = '{:.2f}-{:.2f}'.format(nmin/divisor, nmax/divisor) ax.text( x_r + x_text_nudge, y - y_text_nudge, label, horizontalalignment='left', transform=proj_lat_lon, size=10) plt.title(title_cols[c], fontsize = 14) output_file = os.path.join(config['paths']['figures'], 'water_flow-map-transfer-road-10-shift-{}.png'.format(column)) save_fig(output_file) plt.close()
class Creature: """ Generates a complete virtual creature Tests ----------- """ def __init__(self, params): """ Initialises a simple L-system Parameters ---------- variables : str a string containing all of the letters that take part in the recursion. These letters should also have associated rules. constants : str or None a string containing all the letters that do not take part in the recursion. These letters will not have an associated rule axiom : str The initial character string rules : dict a dictionary containing the rules for recursion. This is a dictionary of listing the letter replacement in the recursion. eg. {"A": "AB", "B": "A"} """ self.Choices = [] self.Params = params self.Rules = params.get('rules') self.L_string = params.get('axiom') self.Constants = params.get('constants') self.Variables = params.get('variables') self.Angle = radians(params.get('angle')) self.recur(params.get('recurs')) self.Length = params.get('length') self.mapper() if "[" in self.Constants: self.tolines() else: self.Lines = [] self.layout() self.results() def recur(self, iters): for _ in range(iters + 1): if self.L_string.count('X') == 0: break else: self.L_string = ''.join( [self.next_char(c) for c in self.L_string]) def next_char(self, c): if c not in self.Rules: return c d = self.Rules[c] r = int(random.random() * len(d)) self.Choices.append(r) return d[r + 1] def mapper(self): """Converts L-string to coordinates Returns ------- List List of coordinates """ num_chars = len(self.L_string) coords = np.zeros((num_chars + 1, 4), dtype=object) nodes = np.zeros_like(coords) rotVec = np.array(((cos(self.Angle), -sin(self.Angle), 0), (sin(self.Angle), cos(self.Angle), 0), (0, 0, 1))) start_vec = np.array((0, 1, 0), np.float64) curr_vec = start_vec i = 1 for c in self.L_string: if c == 'F': coords[i, :3] = (coords[i - 1, :3] + (self.Length * curr_vec)) i += 1 if c == '-': curr_vec = np.dot(curr_vec, (-1 * rotVec)) if c == '+': curr_vec = np.dot(curr_vec, rotVec) if c == '[': nodes = np.vstack((nodes, coords[i - 1])) coords[i - 1, 3] = 'SAVED' nodes[-1, 3] = 'NODE' if c == ']': if coords[i - 1, 3] == 'NODE': coords[i - 1] = nodes[-1] else: coords[i - 1, 3] = 'BRANCH' if len(nodes) == 1: coords[i] = nodes[-1] else: value, nodes = nodes[-1], nodes[:-1] coords[i] = value i += 1 if c == '_': break coords = np.delete(coords, np.s_[i:], 0) for ind, line in enumerate(coords): if line[3] == 'BRANCH': if (line[0:3] == coords[ind + 1, 0:3]).all(): np.delete(coords, ind, 0) self.Coords = coords def tolines(self): """Converts L-string coordinates to individual line segments Returns ------- List List of L-string lines """ lines = [] j = 0 for i in range(len(self.Coords)): if (self.Coords[i, 3] == 'BRANCH') or (i == (len(self.Coords) - 1)): lines.append(self.Coords[j:i + 1, :2].tolist()) j = i + 1 if not lines: self.Lines = [self.Coords[:]] else: self.Lines = [line for line in lines if len(line) > 1] def layout(self): """Converts coordinates or lines to shapely object and buffers Returns ------- Shapely object L-creature "body" """ if len(self.Lines) > 1: self.Linestring = MultiLineString(self.Lines) overall_len = self.Linestring.length max_len = max([line.length for line in self.Linestring]) / overall_len else: self.Linestring = LineString(self.Coords[:, :2]) max_len = 1 self.Area = self.Linestring.buffer(self.Length / 2).area self.Bounds = self.Linestring.bounds self.Penalty = self.Area * max_len def results(self): chars = set(list(self.Constants + self.Variables)) avgs = dict() for char in chars: avgs[char] = re.findall( '(?<=' + re.escape(char) + ').*?(?=' + re.escape(char) + ')', self.L_string) for char in chars: if len(avgs[char]) == 0: avgs[char] = 0.0 else: avgs[char] = sum([len(char) for char in avgs[char]]) / len(avgs[char]) for char in chars: setattr(self, 'Percent' + char, self.L_string.count(char) / len(self.L_string)) setattr(self, 'Count' + char, self.L_string.count(char)) setattr(self, 'Average' + char, avgs.get(char)) try: setattr( self, 'Max' + char, max( len(s) for s in re.findall(r'\[' + char + '\]+', self.L_string))) except: setattr(self, 'Max' + char, 0) self.Comp = np.linalg.norm(self.Bounds) self.Fitness = self.Area / self.Comp self.Rules = list(self.Rules['X'].values()) self.Efficiency = self.Area self.Ratio = np.array([ (self.Choices.count(1) / len(self.Choices)) * getattr(self, self.Params.get('fitness_metric')), (self.Choices.count(2) / len(self.Choices)) * getattr(self, self.Params.get('fitness_metric')), ]) self.Generation = self.Params.get('Generation') self.absorbA = self.Area
spots, points, lines = [], [], [] buffer_distance = 0.0001 for feature in js['features']: if feature['geometry'] and feature['geometry']['type'] == 'Point': x = feature['geometry']['coordinates'][0] y = feature['geometry']['coordinates'][1] p = Point(x,y) points.append(p) spots.append(p.buffer(buffer_distance)) for feature in js['features']: if feature['geometry'] and feature['geometry']['type'] == 'LineString': l = LineString(feature['geometry']['coordinates']) lines.append(l) spots.append(l.buffer(buffer_distance)) patches = cascaded_union(spots) # see also: object.symmetric_difference(other) import copy patches = [] for spot1 in spots: sfinal = copy.deepcopy(spot1) # for spot2 in spots: # if spot1 != spot2: # sfinal = spot1.difference(spot2) patches.append(sfinal) features = [] for p in patches: