python - Need another set of eyes on my loops -



python - Need another set of eyes on my loops -

starting list of coordinates, i'm trying create new list interpolated coordinates included. i'm missing , appends first , lastly coordinate on , on again.

the problem in main function , has replacing origin point newly created point. i've included others because they're necessary. if run this, create .kml file can open in google earth , see problem.

import math geopy import distance import simplekml def build_kml_points(filename, coord_list): kml = simplekml.kml() name = 1 coord_pair in coord_list: kml.newpoint(name=str(name), coords=[(coord_pair[1], coord_pair[0])]) name += 1 kml.save(str(filename)) def bearing(pointa, pointb): # calculates bearing between 2 points. # # :parameters: # - `pointa: tuple representing latitude/longitude # first point. latitude , longitude must in decimal degrees # - `pointb: tuple representing latitude/longitude # sec point. latitude , longitude must in decimal degrees # # :returns: # bearing in degrees # # :returns type: # float # if (type(pointa) != tuple) or (type(pointb) != tuple): # raise typeerror("only tuples supported arguments") lat1 = math.radians(pointa[0]) lat2 = math.radians(pointb[0]) difflong = math.radians(pointb[1] - pointa[1]) x = math.sin(difflong) * math.cos(lat2) y = math.cos(lat1) * math.sin(lat2) - (math.sin(lat1) * math.cos(lat2) * math.cos(difflong)) initial_bearing = math.atan2(x, y) # have initial bearing math.atan2 homecoming values # -180 + 180 not want compass bearing # solution normalize initial bearing shown below initial_bearing = math.degrees(initial_bearing) compass_bearing = (initial_bearing + 360) % 360 homecoming compass_bearing # vincenty's direct formulae def vinc_pt(phi1, lembda1, alpha12, s ) : """ returns lat , long of projected point , reverse azimuth given reference point , distance , azimuth project. lats, longs , azimuths passed in decimal degrees returns ( phi2, lambda2, alpha21 ) tuple f = flattening of ellipsoid: 1/298.277223563 = length of semi-major axis (radius @ equator: 6378137.0) phi1 = latitude of starting point lembda1 = longitude of starting point alpha12 = azimuth (bearing) @ starting point s = length project next point """ f = 1/298.277223563 = 6378137.0 pid4 = math.atan( 1.0 ) two_pi = pid4 * 8.0 phi1 = phi1 * pid4 / 45.0 lembda1 = lembda1 * pid4 / 45.0 alpha12 = alpha12 * pid4 / 45.0 if ( alpha12 < 0.0 ) : alpha12 = alpha12 + two_pi if ( alpha12 > two_pi ) : alpha12 = alpha12 - two_pi # length of semi-minor axis (radius @ poles) b = * (1.0 - f) tanu1 = (1-f) * math.tan(phi1) u1 = math.atan( tanu1 ) sigma1 = math.atan2( tanu1, math.cos(alpha12) ) sinalpha = math.cos(u1) * math.sin(alpha12) cosalpha_sq = 1.0 - sinalpha * sinalpha u2 = cosalpha_sq * (a * - b * b ) / (b * b) = 1.0 + (u2 / 16384) * (4096 + u2 * (-768 + u2 * \ (320 - 175 * u2) ) ) b = (u2 / 1024) * (256 + u2 * (-128 + u2 * (74 - 47 * u2) ) ) # starting approx sigma = (s / (b * a)) last_sigma = 2.0 * sigma + 2.0 # impossible # iterate next 3 eqs unitl no sig alter in sigma # two_sigma_m , delta_sigma while ( abs( (last_sigma - sigma) / sigma) > 1.0e-9 ) : two_sigma_m = 2 * sigma1 + sigma delta_sigma = b * math.sin(sigma) * ( math.cos(two_sigma_m) \ + (b/4) * (math.cos(sigma) * \ (-1 + 2 * math.pow( math.cos(two_sigma_m), 2 ) - \ (b/6) * math.cos(two_sigma_m) * \ (-3 + 4 * math.pow(math.sin(sigma), 2 )) * \ (-3 + 4 * math.pow( math.cos (two_sigma_m), 2 ))))) \ last_sigma = sigma sigma = (s / (b * a)) + delta_sigma phi2 = math.atan2 ( (math.sin(u1) * math.cos(sigma) + math.cos(u1) * math.sin(sigma) * math.cos(alpha12) ), \ ((1-f) * math.sqrt( math.pow(sinalpha, 2) + pow(math.sin(u1) * math.sin(sigma) - math.cos(u1) * math.cos(sigma) * math.cos(alpha12), 2)))) lembda = math.atan2( (math.sin(sigma) * math.sin(alpha12 )), (math.cos(u1) * math.cos(sigma) - math.sin(u1) * math.sin(sigma) * math.cos(alpha12))) c = (f/16) * cosalpha_sq * (4 + f * (4 - 3 * cosalpha_sq )) omega = lembda - (1-c) * f * sinalpha * \ (sigma + c * math.sin(sigma) * (math.cos(two_sigma_m) + c * math.cos(sigma) * (-1 + 2 * math.pow(math.cos(two_sigma_m),2) ))) lembda2 = lembda1 + omega alpha21 = math.atan2 ( sinalpha, (-math.sin(u1) * math.sin(sigma) + math.cos(u1) * math.cos(sigma) * math.cos(alpha12))) alpha21 = alpha21 + two_pi / 2.0 if ( alpha21 < 0.0 ) : alpha21 = alpha21 + two_pi if ( alpha21 > two_pi ) : alpha21 = alpha21 - two_pi phi2 = phi2 * 45.0 / pid4 lembda2 = lembda2 * 45.0 / pid4 alpha21 = alpha21 * 45.0 / pid4 homecoming phi2, lembda2, alpha21 def main(): coord_list = [[40.081059133213, -105.28215], [40.081188699819, -105.28215], [40.081318266425, -105.28215]] point_list = [] x = 1 running_dist = 0 while x < 3: origin = coord_list[x-1] destination = coord_list[x] # append point original list point_list.append(origin) point_dist = distance.distance(origin, destination).km point_dist = float(point_dist[:-3]) init_bearing = bearing(origin, destination) if running_dist < point_dist: new_point = vinc_pt(origin[0], origin[1], init_bearing, 3) point_list.append([new_point[0], new_point[1]]) running_dist += .003 else: x += 1 running_dist = 0 point_list.append(destination) build_kml_points('test.kml', point_list) main()

currently, new list looks this. can see origin , destination appended on , on 1 time again without appending new points.

[[40.081059133213, -105.28215], [40.08108615142624, -105.28215], [40.081059133213, -105.28215], [40.08108615142624, -105.28215], [40.081059133213, -105.28215], [40.08108615142624, -105.28215], [40.081059133213, -105.28215], [40.08108615142624, -105.28215], [40.081059133213, -105.28215], [40.08108615142624, -105.28215], [40.081059133213, -105.28215], [40.081188699819, -105.28215], [40.081318266425, -105.28215], [40.08129124821285, -105.28215], [40.081318266425, -105.28215], [40.08129124821285, -105.28215], [40.081318266425, -105.28215], [40.08129124821285, -105.28215], [40.081318266425, -105.28215], [40.08129124821285, -105.28215], [40.081318266425, -105.28215], [40.08129124821285, -105.28215], [40.081318266425, -105.28215], [40.081188699819, -105.28215]]

expected result: list of coordinates (including origin , destination) between origin , destination @ 3m intervals.

this line in code:

if running_dist < point_dist:

it looks running_dist 0 @ point, , imagine point_dist greater 0, meaning part of loop never run.

python loops if-statement while-loop geopy

Comments

Popular posts from this blog

php - Android app custom user registration and login with cookie using facebook sdk -

django - Access session in user model .save() -

php - .htaccess Multiple Rewrite Rules / Prioritizing -