Python string smoothing algorithm

I am studying linear generalization, which will be used to obtain a generalized road map from a large-scale map to a small-scale map. I use two operating modes and two algorithms. This is done in the python programming language using the shapefile library, this is for vector data in 2d. Operation: selection and elimination. For selection, I use a condition like, all roads, a width of more than 7 meters is selected, this is due to the attribute characteristics of the roads. The same with elimination, like all roads, width less than 5 meters, eliminated. So far this has not been a big problem.

After the selection and exclusion operations are applied, we will have a form file that has passed the condition. I use two algorithms: line simplification and line alignment. To simplify the line, I use the Douglas-Picker line simplification algorithm. it receives vector data (coordinates) and is based on tolerance, removing some points. I am doing this using the Python programming language. After obtaining simplified lines, some editing is required, for example, smoothing the line. Here I use the Gauss algorithm, however it returns some error, which I do not understand, since I am new to the programming environment

import numpy ### This is the Gaussian data smoothing function I wrote ### def smoothListGaussian(list1,degree): window=degree*2-1 weight=numpy.array([1.0]*window) print weight weightGauss=[] for i in range(window): i=i-degree+1 frac=i/float(window) gauss=1/(numpy.exp((4*(frac))**2)) weightGauss.append(gauss) print weightGauss weight=numpy.array(weightGauss)*weight print weight print len(list1)-window smoothed=[0.0]*(len(list1)-window) print smoothed for i in range(len(smoothed)): smoothed[i]=sum(numpy.array(list1[i:i+window])*weight)/sum(weight) return smoothed a=[[78.03881018900006, 30.315651467000066], [78.044901609000078, 30.31512798600005], [78.04927981700007, 30.312510579000048], [78.050041244000056, 30.301755415000059], [78.072646124000073, 30.281720353000082], [78.07902308000007, 30.273344651000059]] smoothListGaussian(a,3) 

Any ideas please. Or if there are any other algorithms in python that smooth out lines in vector data using the coordinates of each point in a string

Any answers appreciated!

+2
source share
2 answers

I think you used the code here . You should have paid attention to the fact that the code is intended for individual measurement data points not for multidimensional data points.

I am not very well versed in the Gaussian smoothing algorithm, but after only briefly looking at your code, I believe that the following is what you are trying to do (I'm not sure if this gives you the result of the desire). Replace the last part of the code with the following code:

 smoothed=[0.0,0.0]*(len(list1)-window) print smoothed for i in range(len(smoothed)): smoothing=[0.0,0.0] for e,w in zip(list1[i:i+window],weight): smoothing=smoothing+numpy.multiply(e,w) smoothed[i]=smoothing/sum(weight) 
+1
source

You can smooth the path with the following code:

 from scipy.ndimage import gaussian_filter1d import numpy as np a=np.array([[78.03881018900006, 30.315651467000066], [78.044901609000078, 30.31512798600005], [78.04927981700007, 30.312510579000048], [78.050041244000056, 30.301755415000059], [78.072646124000073, 30.281720353000082], [78.07902308000007, 30.273344651000059]]) x, y = aT t = np.linspace(0, 1, len(x)) t2 = np.linspace(0, 1, 100) x2 = np.interp(t2, t, x) y2 = np.interp(t2, t, y) sigma = 10 x3 = gaussian_filter1d(x2, sigma) y3 = gaussian_filter1d(y2, sigma) x4 = np.interp(t, t2, x3) y4 = np.interp(t, t2, y3) plot(x, y, "o-", lw=2) plot(x3, y3, "r", lw=2) plot(x4, y4, "o", lw=2) 

Here is the result: the blue dots are the source data, the red curve is the smoothed curve that contains many dots, if you want the point of delivery to be like the source data, you can try with the red curve and get green dots.

You can set sigma to change the smooth level of gaussian_filter1d() .

enter image description here

+13
source

All Articles