def init(self, hqDays=192): hqRepo = getDayFolder() hq = csvInput(ticker, hqRepo) print(len(hq)) shift_left = 0 # this shifts the window (size of hqDays) to left offset = len(hq) - hqDays - shift_left hq = np.array(hq[offset:offset + hqDays]) # print(hq) """ HQ 1d array """ # self.volHist = hq[:, 5].astype(np.int) # volume history # print(self.VHist[:6]) self._CHist = hq[:, 3].astype(np.float) # close history cIdxHist = np.vstack((range(len(self.CHist)), self.CHist)).T.tolist() # dayIdxs = range(len(closeHist)) # to2dim = np.vstack((dayIdxs, self.CHist)).T # epsilon = self.lastC * .1 # rdpList = rdp(to2dim.tolist(), epsilon) """ RDP data """ self.hqEpsilon = self.lastC * .07 self.hqRdp = rdp(cIdxHist, self.hqEpsilon) self.rdpGroups, self.rdpGroupAvgs = self.hqGrouping(self.hqRdp) print(self.rdpGroups, self.rdpGroupAvgs) # print(self.hqRdp) self._lastNGroups, self._lastNGroupAvgs = self.hqGrouping( rdp(cIdxHist[len(cIdxHist) - self.lastN:], self.hqEpsilon / 2)) print(self._lastNGroups, self._lastNGroupAvgs)
def resample(feature: Dict, resampling_resolution: float=0.001): # downsample the coordinates using the rdp algorithm, mainly to reduce 50 megabyte to a about 150 kilobytes. # The code is a little bit dirty, using these counters. If you can refactor, please do :) log.info("Resampling path for %s" % feature["properties"]["name"]) if feature["geometry"]["type"] == "Polygon": log.debug("Original length: %s" % len(feature["geometry"]["coordinates"][0])) i = 0 for coordinate in feature["geometry"]["coordinates"]: feature["geometry"]["coordinates"][i] = rdp(coordinate, epsilon=resampling_resolution) i += 1 log.debug("Resampled length: %s" % len(feature["geometry"]["coordinates"][0])) if feature["geometry"]["type"] == "MultiPolygon": i, j = 0, 0 for coordinate in feature["geometry"]["coordinates"]: for nested_coordinate in feature["geometry"]["coordinates"][i]: feature["geometry"]["coordinates"][i][j] = rdp(nested_coordinate, epsilon=resampling_resolution) j += 1 j = 0 i += 1 return feature
def simplify_path(path, e=1, tp_indices=None): start = path[0] end = path[-1] if tp_indices is None: points = rdp(path, epsilon=e) points = [(x[0], x[1]) for x in points] simplified_path = fill(points, start, end) else: indices = tp_indices + [len(path) - 1] points = [start] n = 0 for i in indices: points_segment = rdp(path[n:i + 1], epsilon=e) points_segment = [(x[0], x[1]) for x in points_segment] points.extend(points_segment[1:]) n = i simplified_path = fill(points, start, end) del tp_indices[:] for i in indices: tp_indices.append(simplified_path.index(path[i])) if len(tp_indices) > 0: del tp_indices[-1] return simplified_path, points
def smooth(inp_vector, count_of_nodes): for epsilon in [ 50, 10, 8, 5, 3.5, 3, 2.5, 2, 1.5, 1, 0.8, 0.7, 0.5, 0.4, 0.3, 0.2, 0.1 ]: if len(rdp(inp_vector, epsilon)) > count_of_nodes: return rdp(inp_vector, epsilon)
def reduce_points(points_list, modifier = 2, tolerance = 1.0/10**6, max_points = 30000, min_points = 29000): from rdp import rdp #Ramer-Douglas-Peucker, roughly 30,000 points permitted by google points = rdp(points_list, tolerance) while((len(points) > max_points) or (len(points) < min_points)): points = rdp(points_list, tolerance) if(len(points) > 30000): tolerance = tolerance * modifier elif(len(points) < 29000): tolerance = tolerance / modifier modifier = modifier * 0.95 if(modifier < 1.01): modifer = 1.01 return(points)
def get_path(matches, obstacles, img, color): print matches list_of_obs = [] k = 15 o = obstacles for obs in o: for j in range(obs[2][1] - k, obs[2][1] + obs[2][3] + k): for i in range(obs[2][0] - k, obs[2][0] + obs[2][2] + k): list_of_obs.append((i, j)) list_of_gridpoints, map_dict, rmapd = grph.grid(img, 20) temp = [] for pnt in list_of_gridpoints: if pnt in list_of_obs: temp.append(pnt) list_of_obs = temp list_of_mapped_obs = [] for o in list_of_obs: list_of_mapped_obs.append(map_dict[o]) path = [] listres = [] print map_dict, color, matches mapped_get_front = map_dict[mapped_nearest(get_front(), list_of_gridpoints)] mapped = map_dict[mapped_nearest(matches[color[0]][1], list_of_gridpoints)] path = rdp( djikstra.getPath(mapped_get_front, mapped, 20, list_of_mapped_obs)) path.append(mapped) listres.append(mapped) for i in range(1, len(color)): if type(matches[color[i]]) is not type(9): mapped2 = map_dict[mapped_nearest(matches[color[i]][1], list_of_gridpoints)] path = path + rdp( djikstra.getPath(mapped, mapped2, 20, list_of_mapped_obs)) path.append(mapped2) listres.append(mapped2) mapped = mapped2 print path for j, point in zip(range(0, len(path)), path): path[j] = rmapd[(point[0], point[1])] print path for j, point in zip(range(0, len(listres)), listres): listres[j] = rmapd[(point[0], point[1])] return listres, path
def reduceAndWriteSegments(self, segments, headerline, epsilon): if len(segments) == 0: return if len(segments) == 1: arr = headerline.split() newpoints = 1 self.fout.write('segment {} rank {} points {}\n'.format( arr[1], arr[3], newpoints)) v = segments[0] self.fout.write('\t{:9.6f} {:9.6f}\n'.format(v[0], v[1])) return # reduce # if too large, use segments[0:5000,:] etc reduced = rdp(segments, epsilon=epsilon) # write headerline arr = headerline.split() newpoints = len(reduced) self.fout.write('segment {} rank {} points {}\n'.format( arr[1], arr[3], newpoints)) # write segments for v in reduced: self.fout.write('\t{:9.6f} {:9.6f}\n'.format(v[0], v[1])) # report logging.info("segment {:3d} completed. reduced polygon " "from {:4d} points to {:4d} points".format( int(arr[1]), int(arr[5]), int(newpoints)))
def on_simplify_button_click(self, checked=False, epsilon=None): if self.in_waypoints is None: return if epsilon is None: ep = self.epsilon() else: ep = epsilon temp_waypoints = np.c_[self.in_waypoints['x'], self.in_waypoints['y'], self.in_waypoints['z']] waypoint_mask = rdp.rdp(temp_waypoints, epsilon=ep, return_mask=True) out_waypoints = temp_waypoints[waypoint_mask] self.global_dict['disc_out_waypoints'] = dict(yaw=None) self.global_dict['disc_out_waypoints']['x'] = out_waypoints[:, 0] self.global_dict['disc_out_waypoints']['y'] = out_waypoints[:, 1] self.global_dict['disc_out_waypoints']['z'] = out_waypoints[:, 2] self.global_dict['disc_mask'] = np.where(waypoint_mask)[0] if ep == 0.: self.global_dict['disc_out_waypoints']['yaw'] = self.in_waypoints[ 'yaw'] print("Simplified waypoints")
def simplify(self, epsilon=0.01, verbose=False): """Modifies the instance geojson by reducing its number of points. Runs the Ramer–Douglas–Peucker algorithm to simplify the GeoJSONs. Wikipedia page: wikipedia.org/wiki/Ramer–Douglas–Peucker_algorithm Args: epsilon: The epsilon parameter to the Ramer–Douglas–Peucker algorithm. See the Wikipedia page for details. verbose: If True, the number of points in the GeoJSON before and after simplification will be printed for comparison. """ coords = self.geojson['coordinates'] original_size = 0 simplified_size = 0 # Iterate over polygons. for i in range(len(coords)): assert len(coords[i]) == 1 c = coords[i][0] original_size += len(c) new_c = rdp.rdp(c, epsilon=epsilon) simplified_size += len(new_c) if len(new_c) >= 3: # Simplify the polygon succeeded, not yielding a line coords[i][0] = new_c if verbose: print(f"Original number of points = {original_size}.") print(f"Simplified number of points = {simplified_size}.")
def optimize_segment_rdp(seg): result = gpxpy.gpx.GPXTrackSegment() arr = np.array(list(map(lambda p: [p.latitude, p.longitude], seg.points))) mask = rdp.rdp(arr, algo="iter", return_mask=True, epsilon=rdp_epsilon) parr = np.array(seg.points) result.points = list(parr[mask]) return result
def normalize_and_simplify(strokes, max_num_points, eps=1e-3): from rdp import rdp # First, normalize & pad to range [0, 1] stroke_lens = [len(stroke) for stroke in strokes] points = SketchUtil.normalization(np.concatenate(strokes)) if points is None: return None strokes_norm = np.split(points, np.cumsum(stroke_lens)[:-1], axis=0) # Reduce num of points if np.sum(stroke_lens) <= max_num_points: return strokes_norm # Computer an ordering stroke_idxs = SketchUtil.compute_stroke_orders(strokes_norm) # Use RDP algorithm to simplify strokes_rdp = [rdp(stroke, epsilon=eps) for stroke in strokes_norm] chosen_idxs = list() cnt = 0 for i in stroke_idxs: num_pts = len(strokes_rdp[i]) if cnt + num_pts < max_num_points: chosen_idxs.append(i) cnt += num_pts else: break # ** Restore original order ** strokes_res = [strokes_rdp[i] for i in sorted(chosen_idxs)] return strokes_res
def detect(self, c): shape = "unidentified" peri = madDist(c) epsilon = peri * 0.1 aprox = rdp(c, epsilon) if len(aprox) == 3: shape = "triangulo" elif len(aprox) == 4: (x, y, w, h) = cv2.boundingRect(aprox) ar = w / float(h) shape = "cuadrado" if ar >= 0.95 and ar <= 1.05 else "rectangulo" elif len(aprox) == 5: shape = "pentagono" elif len(aprox) == 6: shape = "hexagono" elif len(aprox) == 7: shape = "heptagono" else: shape = "circulo" return shape
def read_svg(svg_path, scale=100.0, draw_mode=False): """ read svg, centralised and convert to stroke-3 format scale: stroke-3 output having max dimension [-scale, +scale] """ try: paths, path_attrs = svg2paths( svg_path, return_svg_attributes=False) # svg to paths lines = [] lens = [] for path_id, path in enumerate(paths): # get poly lines from path erase = False # path could be erased by setting stroke attribute to #fff (sketchy) path_attr = path_attrs[path_id] if 'stroke' in path_attr and path_attr['stroke'] == '#fff': erase = True # try: plen = int(path.length()) # except ZeroDivisionError: # plen = 0 if plen > 0 and not erase: lines.append( [path.point(i) for i in np.linspace(0, 1, max(2, plen))]) lens.append(plen) # convert to (x,y) coordinates lines = [ np.array([[real(x), imag(x)] for x in path]) for path in lines ] # get dimension of this drawing tmp = np.concatenate(lines, axis=0) w_max, h_max = np.max(tmp, axis=0) w_min, h_min = np.min(tmp, axis=0) w = w_max - w_min h = h_max - h_min max_hw = max(w, h) def group(line): out = np.array(line, dtype=np.float32) out[:, 0] = ((out[:, 0] - w_min) / max_hw * 2.0 - 1.0) * scale out[:, 1] = ((out[:, 1] - h_min) / max_hw * 2.0 - 1.0) * scale return out # normalised lines = [group(path) for path in lines] lines_simplified = [rdp(path, epsilon=1.5) for path in lines] # apply RDP algorithm strokes_simplified = lines_to_strokes( lines_simplified) # convert to 3-stroke format (dx,dy,pen_state) # scale_bound(strokes_simplified, 10) if draw_mode: draw_strokes3(strokes_simplified, 1.0) # no need to concat the origin point print('num points: {}'.format(len(strokes_simplified))) return np.array(strokes_simplified, dtype=np.float32) except Exception as e: print('Error encountered: {} - {}'.format(type(e), e)) print('Location: {}'.format(svg_path)) raise
def wo_wlines(all_path): line = [] for i in range(len(all_path)): clr = all_path[i].getAttribute('stroke') if clr == '#000': d = all_path[i].getAttribute('d') P = parse_path(d) points = [] if len(P) < 1: pass else: for j in range(len(P)): if isinstance(P[j], CubicBezier) or isinstance(P[j], Line): strt = P[j].start.real, P[j].start.imag ed = P[j].end.real, P[j].end.imag if j == 0: points.append(strt) points.append(ed) else: points.append(ed) else: print("What?! th is " + P[j]) points = rdp(points, epsilon=0.5) line.append(points) else: pass return line
def build_control(self): """ Build the control dataset from the NavExport dataframe """ drop_columns = [ x for x in list(self._data.columns) if x not in control_cols ] logging.debug("Dropping columns: %s", drop_columns) self._data = self._data.drop(drop_columns, axis=1) # run rdp algorithim logging.debug("Building control coordinates using RDP algorithim") coords = self._data.filter(['ship_longitude', 'ship_latitude'], axis=1).to_numpy() control = rdp(coords, epsilon=RDP_EPSILON) logging.debug("Length of full-res coordinates: %d", len(self._data.index)) logging.debug("Length of control coordinates: %d", control.shape[0]) control_df = pd.DataFrame(control, columns=['ship_longitude', 'ship_latitude']) self._data = pd.merge(control_df, self._data, on=['ship_longitude', 'ship_latitude'], how='left') self._data = self._data[control_cols] logging.debug("Rounding data: %s", rounding) self._data = self._round_data(self._data, rounding) # Update geocsv header self._geocsv_header = control_header
def simplify_paths_rdp(slicer, threshold): """Simplifies a path using the Ramer–Douglas–Peucker algorithm, implemented in the rdp python library. https://en.wikipedia.org/wiki/Ramer-Douglas-Peucker_algorithm Parameters ---------- slicer: :class:`compas_slicer.slicers.BaseSlicer` An instance of one of the compas_slicer.slicers classes. threshold: float Controls the degree of polyline simplification. Low threshold removes few points, high threshold removes many points. """ logger.info("Paths simplification rdp") remaining_pts_num = 0 with progressbar.ProgressBar(max_value=len(slicer.layers)) as bar: for i, layer in enumerate(slicer.layers): if not layer.is_raft: # no simplification necessary for raft layer for path in layer.paths: pts_rdp = rdp.rdp(np.array(path.points), epsilon=threshold) path.points = [ Point(pt[0], pt[1], pt[2]) for pt in pts_rdp ] remaining_pts_num += len(path.points) bar.update(i) logger.info('%d Points remaining after rdp simplification' % remaining_pts_num)
def computeRdpForMean(wl, data, Y, justbymoda): """ fonction qui permet de calculer le rdp associé à la moyenne des reflectances par modas, on peut baisser ou augmenter le seuil pour augmenter ou baisser respectivement le nombre de longueur d'onde :param wl: longueurs d'onde :param data: données instanciers :param Y: classe des données instanciers :param justbymoda: booleen associé a la regression pls :return: liste des données rdp """ meandef = [] for deficiency in listeDef: meandef.append({ 'Moda': deficiency, 'Reflectance': computeMeanByModa(data, deficiency, Y, justbymoda) }) for meansample in meandef: i = 0 listeCouple = [] while i < len(wl): listeCouple.append([float(wl[i]), meansample['Reflectance'][i]]) i += 1 meansample['Reflectance'] = rdp(listeCouple, epsilon=0.001) return meandef
def routeBetween( self, p1: tuple(), p2: tuple(), altitude: float() = 120, epsilon: float() = 0, absolute: bool() = False, ) -> list(): idx1 = self.tree.query(p1)[1] idx2 = self.tree.query(p2)[1] if idx1 < idx2: if idx2 != len(self.points3D): points = self.points3D[idx1:idx2 + 1] else: point = self.points3D[idx1:] else: if idx2 != 0: points = self.points3D[idx1:idx2 - 1:-1] else: points = self.points3D[idx1::-1] if absolute: points = [(point[0], point[1], point[2] + altitude) for point in points] else: points = [(point[0], point[1], altitude) for point in points] points_rdp = rdp(points, epsilon=epsilon) return points_rdp
def roc(truth_probabilities: pd.Series, prediction_probabilities: pd.Series, weights: pd.Series) -> pd.DataFrame: fprs, tprs, thresholds = sklearn.metrics.roc_curve( truth_probabilities, prediction_probabilities, sample_weight=weights) roc = pd.DataFrame({ 'fpr': fprs, 'tpr': tprs }, index=thresholds, columns=['fpr', 'tpr']) if len(fprs) > 100: # simplify line using Ramer-Douglas-Peucker algorithm if more than 100 points points = np.vstack((fprs, tprs)).T # a simple test reduced a roc curve of 2161 items to # epsilon 0 ... 660 # epsilon 0.0001 ... 573 # epsilon 0.0005 ... 344 # epsilon 0.001 ... 197 # epsilon 0.005 ... 17 mask = rdp(points, return_mask=True, epsilon=0.001) roc = roc[mask] return roc
def rdp_lines(xy_lines,ep= 0.8): all_lines = [] current_line = [] for xy in xy_lines: x = int(xy[0]) y = int(xy[1]) eos = xy[2] current_line.append([x,y]) if eos == 1: simple_line = np.array(rdp(current_line,epsilon=ep)) #higher epsilon = more simplified h,w = np.shape(simple_line) z = np.zeros((h,1),dtype = simple_line.dtype) simple_line = np.hstack((simple_line,z)) h,w = np.shape(simple_line) simple_line[h-1,w-1] = 1 if len(all_lines) == 0: all_lines = simple_line else: all_lines = np.concatenate((all_lines,simple_line)) current_line = [] else: continue try: h,w = np.shape(all_lines) z = np.zeros((h,1),dtype = all_lines.dtype) all_lines = np.hstack((all_lines,z)) return all_lines except: return xy_lines
def apply_rdp_filter(ride): len_before = len(ride.raw_coords_filtered) mask = rdp(ride.raw_coords_filtered, RDP_EPSILON, return_mask=True) ride.raw_coords_filtered = filter_by_mask(ride.raw_coords_filtered, [not boolean for boolean in mask]) ride.timestamps_filtered = filter_by_mask(ride.timestamps_filtered, [not boolean for boolean in mask]) print("RDP filter filtered {} coordinates.".format(len_before - len(ride.raw_coords_filtered))) return ride
def temp_approximation(self, adc_start=100, adc_stop=3900, epsilon=0.3): """ Calculate linear approximation to temperature curve for a range of ADC values. """ adc_values = numpy.arange(adc_start, adc_stop) return rdp(zip(adc_values, self.temp(adc_values)), epsilon=epsilon)
def makeDownsampledTracks(csvDir): csvFilenames = glob.glob(csvDir + '*.csv') for csvFilename in csvFilenames: csvPreviewFilename = csvFilename.replace('\\2016\\', '\\2016\\preview\\').replace( '.csv', '_preview.csv') if os.path.isfile(csvPreviewFilename): continue data = pd.read_csv(csvFilename) lonlat = np.array(data[['lon', 'lat']]) # epsilon=.0005 seems to be a good compromise for displaying a 300px square map # subsampling by ::3 speeds up the RDP algorithm and doesn't reduce any detail lonlat_sub = pd.DataFrame(rdp.rdp(lonlat[::3, :], epsilon=.0005)) lonlat_sub['id'] = data['id'][0] lonlat_sub.columns = ['lon', 'lat', 'id'] writeActivityCSV(lonlat_sub, csvPreviewFilename) print(csvFilename)
def mouseReleaseEvent(self, event): #player move playerNum = self.globalStep % self.numPlayer print("Player %s's turn" % (str(playerNum))) # import ipdb; ipdb.set_trace() new_pos = self.players.predict_next_stroke(self.pos_xy_simplified) # print(self.pos_xy_simplified) self.pos_xy += new_pos # pos_test = (-1, -1) # self.pos_xy.append(pos_test) #update simplified sketch simp = rdp(self.currentStroke(), epsilon=1.0) if (len(simp) > 2): for i in range(len(simp)): if (simp[i][0] <= 0): simp[i] = simp[i - 1] self.pos_xy_simplified.append(simp) #detect the sketch img = np.array(strokes_to_image_str(self.pos_xy_simplified)) if (img.ndim != 0): print self.iu.infer(img, 5) self.update()
def f(): start = time.time() nodes = list(_G.nodes()) random.shuffle(nodes) changed = False for n in nodes: if verbose: delta = time.time() - start if delta > 5: start = time.time() if verbose: print("Ramer-Douglas-Peucker remaining nodes:", len(_G.nodes())) ajacent_nodes = list(nx.neighbors(_G, n)) if n in ajacent_nodes: ajacent_nodes.remove(n) if len(ajacent_nodes) == 2: node_triplet = [ _G.nodes[ajacent_nodes[0]]['pos'], _G.nodes[n]['pos'], _G.nodes[ajacent_nodes[1]]['pos'] ] if len(rdp.rdp(node_triplet, epsilon=epsilon)) == 2: _G.add_edge(*ajacent_nodes) _G.remove_node(n) changed = True return changed
def __linestring_coords(linestring, epsilon): if linestring is None: return None coordinates = mapping(linestring)['coordinates'] points = [pair[0] for pair in coordinates] points.append(coordinates[-1][1]) return tuple([tuple(point) for point in points]) if epsilon is None or epsilon == 0 else \ tuple([tuple(point) for point in rdp(points, epsilon=epsilon)])
def main(): points = np.array(get_points()) epsilon = 1.0 original_rdp = rdp(points, epsilon) custom_rdp = RDP(points, epsilon).run() np.testing.assert_array_equal(custom_rdp, original_rdp) draw_plots(custom_rdp, points) return 0
def find_corners_from_contours(page_contour): """Analyze the largest contour of the image and return the four corners of the document in the image""" # epsilon = 0.1 * cv2.arcLength(page_contour, True) epsilon = 0.00001 * cv2.arcLength(page_contour, True) page_approx = rdp(page_contour, epsilon) # page_approx = cv2.approxPolyDP(page_contour, epsilon, True) return page_approx.sum(axis=1), page_approx
def test_min_num(random_walk): # Set maximum allowable error epsilon = 20.0 G_pp = polyprox.min_num(random_walk, epsilon) G_rdp = rdp.rdp(random_walk, epsilon) assert np.array_equal(G_rdp, G_pp)
def apply_rdp(strokes, epsilon=1.5): """ Apply Ramer-Douglas-Peucker algorithm to an absolute position stroke-3 format. :param strokes: :param epsilon: :return: """ return [rdp(stroke, epsilon=epsilon) for stroke in strokes]
def _simplify_points(klass, polygon, tol=2.0): """ Applies Ramer–Douglas–Peucker algorithm to simplify a room polygon. """ pts = [ tuple(p) for p in polygon.points ] simplified = rdp.rdp(pts, tol) return simplified
def simplify_edges(edges): total_area = get_polygon_area(edges.reshape(-1, 2)) for epsilon in np.arange(0, 20, 0.5): if (len(edges) > 4) and (get_polygon_area(edges.reshape(-1, 2)) > total_area * 0.99): edges = rdp(edges, epsilon=epsilon) else: break return edges
def optimize_segment(seg): """ RDP algorithm """ result = GPXTrackSegment() arr = np.array(list(map(lambda p: [p.latitude, p.longitude], seg.points))) mask = rdp(arr, algo="iter", return_mask=True, epsilon=EPSILON) parr = np.array(list(seg.points)) result.points = list(parr[mask]) return result
def print_gpx_google_polyline(trk,numLevels,zoomFactor,epsilon,forceEndpoints): import rdp zoomLevelBreaks = [] for i in range (0,numLevels): zoomLevelBreaks.append(epsilon*pow(zoomFactor, numLevels-i-1)); word_segments = 6; for seg in trk: if len(seg) == 0: continue; # Perform the RDP magic on the data if (epsilon > 0): seg = rdp.rdp(seg, epsilon); segment_polyline = ""; segment_point = 0; # Encode the points for p in seg: # Get the first point full value, # then the difference between points if (segment_point == 0): lat = (int)(1e5 * p[var_lat]); lon = (int)(1e5 * p[var_lon]); else: lat = (int)(1e5 * (p[var_lat]) - (int)(1e5 * seg[segment_point-1][var_lat])); lon = (int)(1e5 * (p[var_lon]) - (int)(1e5 * seg[segment_point-1][var_lon])); segment_point += 1; segment_polyline += ''.join(polyline_encode_point(lat)); segment_polyline += ''.join(polyline_encode_point(lon)); print ("polyline: %s\n" % segment_polyline) # Encode the levels segment_levels = ""; segment_point = 0; for p in seg: distance = rdp.point_line_distance(p, seg[0], seg[-1]); if ((forceEndpoints == 1) and (segment_point == 0 or segment_point == len(seg))): level = numLevels - 1; else: level = numLevels - find_zoom_level(distance, zoomLevelBreaks) - 1; segment_levels += ''.join(polyline_encode_level(level)); segment_point += 1; print ("levels: %s\n" % segment_levels) print ("\n"); return segment_polyline
def rotue_between_stops(self, start_stop, end_stop): # Find nodes start_lat, start_lon = map(float, self.stops[start_stop]) end_lat, end_lon = map(float, self.stops[end_stop]) start = self.router.findNode(start_lat, start_lon) end = self.router.findNode(end_lat, end_lon) # Do route # SafetyCheck - start and end nodes have to be defined if start and end: try: with time_limit(10): status, route = self.router.doRoute(start, end) except TimeoutError: status, route = "timeout", [] route_points = list(map(self.router.nodeLatLon, route)) # SafetyCheck - route has to have at least 2 nodes if status == "success" and len(route_points) <= 1: status = "to_few_nodes_({d})".format(len(route)) # Apply rdp algorithm route_points = rdp.rdp(route_points, epsilon=0.000006) else: start, end = math.nan, math.nan dist_ratio = math.nan status = "no_nodes_found" # If we failed, catch some more info on why if status != "success": ### DEBUG-SHAPES ### if not os.path.exists("shape-errors/{}-{}.json".format(start_stop, end_stop)): with open("shape-errors/{}-{}.json".format(start_stop, end_stop), "w") as f: json.dump( {"start": start_stop, "end": end_stop, "start_node": start, "end_node": end, "start_pos": [start_lat, start_lon], "end_pod": [end_lat, end_lon], "error": status }, f, indent=2 ) route_points = [[start_lat, start_lon], [end_lat, end_lon]] return route_points
def apply_rdp_profile(self): """ Apply Ramer-Douglas-Peucker Algorithm to reduce number of points in profile """ # Calculate Profile. Can I call calc_profile somehow??? dafp = [] # distance along flight path for i in range(len(self.x)): if i == 0: dafp.append(0) else: dafp.append(math.hypot(self.x[i], self.y[i])) profile = [] for i in range(len(dafp)): profile.append((dafp[i], self.z[i])) # Apply rdp (2-dim) rdp_profile = rdp.rdp(profile, 50) return(rdp_profile)
base_img = problem base_pixels = base_img.load() start_time = time.time() path = BFS(start, end, base_pixels) print str(time.time() - start_time) + " seconds" print str(len(path)) + " path length" greys = 0 for p in base_img.getdata(): if p == (127,127,127): greys += 1 print str(greys) + " greys" rdp_path = rdp(path,epsilon=EPSILON) path_problem = problem path_problem_pixels = path_problem.load() path_raw = raw path_raw_pixels = path_raw.load() rdp_path_problem = problem.copy() rdp_path_problem_pixels = rdp_path_problem.load() rdp_path_raw = raw.copy() rdp_path_raw_pixels = rdp_path_raw.load() connected = True try:
def test_eps1(self): """ Epsilon large enough to be simplified. """ assertAE(rdp(np.array([0, 0, 5, 1, 10, 1]).reshape(3, 2), 1), np.array([0, 0, 10, 1]).reshape(2, 2))
def test_hor(self): """ Horizontal line. """ assertAE(rdp(np.array([0, 0, 1, 0, 2, 0, 3, 0, 4, 0]).reshape(5, 2)), np.array([0, 0, 4, 0]).reshape(2, 2))
def test_ver(self): """ Vertical line. """ assertAE(rdp(np.array([0, 0, 0, 1, 0, 2, 0, 3, 0, 4]).reshape(5, 2)), np.array([0, 0, 0, 4]).reshape(2, 2))
def test_diag(self): """ Diagonal line. """ assertAE(rdp(np.array([0, 0, 1, 1, 2, 2, 3, 3, 4, 4]).reshape(5, 2)), np.array([0, 0, 4, 4]).reshape(2, 2))
import numpy as np import matplotlib.pyplot as plt from rdp import rdp # polyline coordinates coord = [[30, -245], [61, -186], [80, -134], [125, -129], [153, -165], [173, -199], [224, -212], [268, -178], [286, -137], [319, -132], [352, -158], [371, -155], [399, -156], [419, -188], [445, -207], [460, -194], [472, -200], [489, -185], [480, -138], [465, -87], [477, -44], [501, -41], [544, -81], [570, -118]] # implementation of the Ramer-Douglas-Peucker algorithm: https://pypi.python.org/pypi/rdp res100 = rdp(coord, epsilon=100) res50 = rdp(coord, epsilon=50) res25 = rdp(coord, epsilon=25) res10 = rdp(coord, epsilon=10) # convert python array to numpy-stype array np_coord = np.asarray(coord) np_res100 = np.asarray(res100) np_res50 = np.asarray(res50) np_res25 = np.asarray(res25) np_res10 = np.asarray(res10) # plot polylines plt.plot(np_coord[:,0], np_coord[:,1], "ko-") plt.plot(np_res100[:,0], np_res100[:,1], "bx-") plt.plot(np_res50[:,0], np_res50[:,1], "cx-") plt.plot(np_res25[:,0], np_res25[:,1], "gx-") plt.plot(np_res10[:,0], np_res10[:,1], "mx-") # plot tolerance values plt.plot([-50, -50], [-245, -145], "bx-")
for line in streamSegmentsInput: #print("\nworking on a new line now! (%d)" %lineNumber) info = line.split(); length = len(info) # has at least id, start, end, x1, y1, x2, y2, and does not have half a coordinate if(length >= 7): index = 3 coordinates = [] while index + 1 < length: x = float(info[index]) y = float(info[index+1]) coordinates.append([x,y]) #print("getting coordinates at " + str(index)) index += 2 #print (" gathered initial coordinates") reducedCoordinates = rdp(coordinates, epsilon=epsilon) #additional optional parameter: epsilon=epsilon output = "%s %s %s " %(info[0], info[1], info[2]) for item in reducedCoordinates: output += str(item[0]) + " " + str(item[1]) + " " output += "\n" streamSegmentsOutput.write(output) #print("reduced line: " + output + "\n") #print(" wrote the reduced line") if lineNumber%50 == 0: print("just wrote the reduced form of line %d" %lineNumber) lineNumber += 1 print("finished: last written line: %d" %lineNumber) streamSegmentsInput.close() streamSegmentsOutput.close()
def execute(reduce_map,path): print "number before reducing: " + str(len(reduce_map)) print "number after reducing: " + str(len(rdp(reduce_map, epsilon=0.001))) write(rdp(reduce_map, epsilon=0.001),path)
def test_eps0(self): """ Epsilon being to small to be simplified. """ assertAE(rdp(np.array([0, 0, 5, 1, 10, 1]).reshape(3, 2)), np.array([0, 0, 5, 1, 10, 1]).reshape(3, 2))
def Reduce_data(data_X, data_Y, x1): Mx = np.column_stack((data_X[:, np.newaxis], data_Y[:, np.newaxis])) M2 = Red.rdp(Mx, x1) data_X2 = M2[:, 0].transpose() data_Y2 = M2[:, 1].transpose() return data_X2, data_Y2
def simplify_geometry(geom, tolerance): return rdp(geom, epsilon=tolerance)
def test_L0(self): """ Point sequence which has the form of a L. """ assertAE(rdp(np.array([5, 0, 4, 0, 3, 0, 3, 1, 3, 2]).reshape(5, 2)), np.array([5, 0, 3, 0, 3, 2]).reshape(3, 2))
def extract(input_file, output_file): df = pd.read_csv(input_file, sep=",") df=turndf(df) Lxdata=np.array(df['shLX']) Lydata=np.array(df['shLY']) Rxdata=np.array(df['shRX']) Rydata=np.array(df['shRY']) tolerance = 70 min_angle = np.pi*0.11 max_angle = np.pi*0.99 Lpoints = zip(Lxdata,Lydata) Rpoints = zip(Rxdata,Rydata) print(len(Lpoints)) # Use the Ramer-Douglas-Peucker algorithmta to simplify the path # http://en.wikipedia.org/wiki/Ramer-Douglas-Peucker_algorithm # Python implementation: https://github.com/sebleier/RDP/ Lsimplified = np.array(rdp.rdp(Lpoints, tolerance)) Rsimplified = np.array(rdp.rdp(Rpoints, tolerance)) lsx, lsy = Lsimplified.T rsx, rsy = Rsimplified.T #unzip to array # compute the direction vectors on the simplified curve Ldirections = np.diff(Lsimplified, axis=0) Ltheta = angle(Ldirections) # Select the index of the points with the greatest theta # Large theta is associated with greatest change in direction. Lidx = np.where(Ltheta>min_angle)[0]+1 Lidx = np.where(Ltheta<max_angle)[0]+1 # compute the direction vectors on the simplified curve Rdirections = np.diff(Rsimplified, axis=0) Rtheta = angle(Rdirections) print Rtheta # Select the index of the points with the greatest theta # Large theta is associated with greatest change in direction. Ridx = np.where(Rtheta>min_angle)[0]+1 Ridx = np.where(Rtheta<max_angle)[0]+1 with open(output_file, 'a') as f: for x in Ltheta: f.write(fn) f.write(",") f.write("Left") f.write(",") f.write(str(x)) f.write(",") f.write("\n") for x in Rtheta: f.write(fn) f.write(",") f.write("Right") f.write(",") f.write(str(x)) f.write(",") f.write("\n") fig = plt.figure() ax =fig.add_subplot(111) ax.plot(df[['shLX']],df[['shLY']],label='left side') ax.plot(df[['shRX']],df[['shRY']],c='r',label='right side') ax.plot(lsx, lsy, 'g--', label='simplified path') ax.plot(rsx, rsy, 'g--') ax.plot(lsx[Lidx], lsy[Lidx], 'ro', markersize = 10, label='turning points') ax.plot(rsx[Ridx], rsy[Ridx], 'ro', markersize = 10) #ax.invert_yaxis() ax.set_title("P1"+ fn[11:14]+"Turn") plt.legend(loc='best') plt.show() plt.savefig(str(dn)+'\\'+str(fn)+ 'Turn.png')
def ImportAdministrativeBoundary(self, xml): f = None contents = None namespaces = { 'ksj': 'http://nlftp.mlit.go.jp/ksj/schemas/ksj-app', 'gml': 'http://www.opengis.net/gml/3.2', 'xlink': 'http://www.w3.org/1999/xlink', 'xsi': 'http://www.w3.org/2001/XMLSchema-instance' } self._conn.execute('begin') print ('admins....') context = etree.iterparse(xml, events=('end',), tag='{http://nlftp.mlit.go.jp/ksj/schemas/ksj-app}AdministrativeBoundary') for event, admin in context: adminId = admin.get('{http://www.opengis.net/gml/3.2}id') print (adminId) bounds = admin.find('ksj:bounds', namespaces=namespaces).get('{http://www.w3.org/1999/xlink}href')[1:] prefectureName = admin.find('ksj:prefectureName', namespaces=namespaces).text subPrefectureName = admin.find('ksj:subPrefectureName', namespaces=namespaces).text countyName = admin.find('ksj:countyName', namespaces=namespaces).text cityName = admin.find('ksj:cityName', namespaces=namespaces).text areaCode = admin.find('ksj:administrativeAreaCode', namespaces=namespaces).text sql = '''INSERT INTO administrative_boundary (gml_id, bounds, prefecture_name, sub_prefecture_name, county_name, city_name, area_code) VALUES(?, ?, ?, ?, ?, ?, ?);''' self._conn.execute(sql, [adminId, bounds, prefectureName, subPrefectureName, countyName, cityName, areaCode ]) admin.clear() # Also eliminate now-empty references from the root node to <Title> while admin.getprevious() is not None: del admin.getparent()[0] del context print ('surfaces....') context = etree.iterparse(xml, events=('end',), tag='{http://www.opengis.net/gml/3.2}Surface') for event, surf in context: surfId = surf.get('{http://www.opengis.net/gml/3.2}id') print (surfId) curveMembers = surf.xpath('.//gml:curveMember', namespaces=namespaces) for member in curveMembers: memberId = member.get('{http://www.w3.org/1999/xlink}href')[1:] sql = '''INSERT INTO surface (surface_id, curve_member) VALUES(?, ?);''' self._conn.execute(sql, [surfId, memberId ]) surf.clear() # Also eliminate now-empty references from the root node to <Title> while surf.getprevious() is not None: del surf.getparent()[0] del context print ('curves....') context = etree.iterparse(xml, events=('end',), tag='{http://www.opengis.net/gml/3.2}Curve') for event, curve in context: curveId = curve.get('{http://www.opengis.net/gml/3.2}id') posLists = curve.xpath('.//gml:posList', namespaces=namespaces) print (curveId) for posList in posLists: poly = [] points = posList.text.split("\n") for point in points: pt = point.replace("\t", '').split(' ') if len(pt) != 2: continue poly.append([float(pt[0]), float(pt[1])]) polyRdp = rdp(poly, epsilon=0.001) for pt in polyRdp: lat = pt[0] lng = pt[1] sql = '''INSERT INTO curve (curve_id, lat, lng) VALUES(?, ?, ?);''' self._conn.execute(sql, [curveId, lat, lng ]) curve.clear() # Also eliminate now-empty references from the root node to <Title> while curve.getprevious() is not None: del curve.getparent()[0] del context self.Commit()
def test_two(self): """ Point sequence with only two elements. """ assertAE(rdp(np.array([[0, 0], [4, 4]])), np.array([[0, 0], [4, 4]]))
def simplify_coords(coords,tolerance): new_coords = rdp(coords,epsilon=tolerance) new_coords = map(round_me,new_coords) return new_coords
def test_nn(self): """ Non-numpy interface to rdp. """ self.assertEqual(rdp([[0, 0], [2, 2], [4, 4]]), [[0, 0], [4, 4]])