Need help writing code that will automatically write more code?
<p>I need help with writing code for a work project. I have written a script that uses pandas to read an excel file. I have a while-loop written to iterate through each row and append latitude/longitude data from the excel file onto a map (Folium, Open Street Map)</p>

<p>The issue I've run into has to do with the GPS data. I download a CVS file with vehicle coordinates. On some of the vehicles I'm tracking, the GPS loses signal for whatever reason and doesn't come back online for hundreds of miles. This causes issues when I'm using line plots to track the vehicle movement on the map. I end up getting long straight lines running across cities since Folium is trying to connect the last GPS coordinate before the vehicle went offline, with the next GPS coordinate available once the vehicle is back online, which could be hundreds of miles away as <a href="http://imgur.com/a/klX2g" rel="nofollow">shown here</a>. I think if every time the script finds a gap in GPS coords, I can have a new loop generated that will basically start a completely new line plot and append it to the existing map. This way I should still see the entire vehicle route on the map but without the long lines trying to connect broken points together.</p>

<p>My idea is to have my script calculate the absolute value difference between each iteration of longitude data. If the difference between each point is greater than 0.01, I want my program to end the loop and to start a new loop. This new loop would then need to have new variables init. I will not know how many new loops would need to be created since there's no way to predict how many times the GPS will go offline/online in each vehicle.</p>

<p><a href="https://gist.github.com/tapanojum/81460dd89cb079296fee0c48a3d625a7" rel="nofollow">https://gist.github.com/tapanojum/81460dd89cb079296fee0c48a3d625a7</a></p>

<pre><code>import folium
import pandas as pd

#  Pulls CSV file from this location and adds headers to the columns
df = pd.read_csv('Example.CSV',names=['Longitude', 'Latitude',])

lat = (df.Latitude / 10 ** 7)  # Converting Lat/Lon into decimal degrees
lon = (df.Longitude / 10 ** 7)

zoom_start = 17  # Zoom level and starting location when map is opened
mapa = folium.Map(location=[lat[1], lon[1]], zoom_start=zoom_start)

i = 0
j = (lat[i] - lat[i - 1])
location = []
while i &lt; len(lat):
if abs(j) &lt; 0.01:
    location.append((lat[i], lon[i]))
    i += 1
else:
    break

# This section is where additional loops would ideally be generated

# Line plot settings
c1 = folium.MultiPolyLine(locations=[location], color='blue', weight=1.5,         opacity=0.5)
c1.add_to(mapa)

mapa.save(outfile="Example.html")
</code></pre>

<p>Here's pseudocode for how I want to accomplish this.</p>

<p>1) Python reads csv</p>

<p>2) Converts Long/Lat into decimal degrees</p>

<p>3) Init location1</p>

<p>4) Runs while loop to append coords</p>

<p>5) If abs(j) >= 0.01, break loop</p>

<p>6) Init location(2,3,...)</p>

<p>7) Generates new while i &lt; len(lat): loop using location(2,3,...)</p>

<p>9) Repeats step 5-7 while i &lt; len(lat) (Repeat as many times as there are 
instances of abs(j) >= 0.01))</p>

<p>10) Creats (c1, c2, c3,...) = folium.MultiPolyLine(locations=[location], color='blue', weight=1.5, opacity=0.5) for each variable of location</p>

<p>11) Creates c1.add_to(mapa) for each c1,c2,c3... listed above</p>

<p>12) mapa.save</p>

<p>Any help would be tremendously appreciated!</p>

<p><strong>UPDATE:</strong>
Working Solution</p>

<pre><code>import folium
import pandas as pd

#  Pulls CSV file from this location and adds headers to the columns
df = pd.read_csv(EXAMPLE.CSV',names=['Longitude', 'Latitude'])

lat = (df.Latitude / 10 ** 7)  # Converting Lat/Lon into decimal degrees
lon = (df.Longitude / 10 ** 7)

zoom_start = 17  # Zoom level and starting location when map is opened
mapa = folium.Map(location=[lat[1], lon[1]], zoom_start=zoom_start)

i = 1
location = []
while i &lt; (len(lat)-1):
location.append((lat[i], lon[i]))
i += 1
j = (lat[i] - lat[i - 1])
 if abs(j) &gt; 0.01:
    c1 = folium.MultiPolyLine(locations=[location], color='blue',    weight=1.5, opacity=0.5)
    c1.add_to(mapa)
    location = []

mapa.save(outfile="Example.html")
</code></pre>

<p>Your while loop looks wonky.  You only set j once, outside the loop.  Also, I think you want a list of line segments. Did you want something like this;</p>

<pre><code>i = 0
segment = 0
locations = []
while i &lt; len(lat):
    locations[segment] = []  # start a new segment

    # add points to the current segment until all are 
    # consumed or a disconnect is detected
    while i &lt; len(lat):
        locations[segment].append((lat[i], lon[i]))
        i += 1
        j = (lat[i] - lat[i - 1])
        if abs(j) &gt; 0.01:
            break
    segment += 1
</code></pre>

<p>When this is done <code>locations</code> will be a list of segments, e.g.;</p>

<pre><code> [ segment0, segment1, ..... ]
</code></pre>

<p>each segment will be a list of points, e.g.;</p>

<pre><code> [ (lat,lon), (lan,lon), ..... ]
</code></pre>

python automation openstreetmap folium