Skip to content

Instantly share code, notes, and snippets.

View bogardpd's full-sized avatar

Paul Bogard bogardpd

View GitHub Profile
@bogardpd
bogardpd / import_gpx.py
Created February 10, 2021 01:00
Script to merge a GPX file into a KML file.
"""Imports a GPX file into the canonical KML file."""
import os
import sys
import traceback
import gpxpy
import simplekml
from datetime import datetime, timezone
from dateutil.parser import parse
from lxml import etree
@bogardpd
bogardpd / population-gravity.py
Created January 14, 2021 03:33
A script to calculate demographic gravitational force from a spreadsheet of cities to a given city.
"""Calculates demographic gravitation for a spreadsheet of city data.
This script is specifically designed to work with the basic US cities
spreadsheet at https://simplemaps.com/data/us-cities. However, it could
be made to work with any spreadsheet of cities, latitudes, longitudes,
and populations as long as the sheet name and column names are updated
appropriately.
"""
import pandas as pd
<?xml version="1.0" encoding="UTF-8"?>
<graphml xmlns="http://graphml.graphdrawing.org/xmlns"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://graphml.graphdrawing.org/xmlns
http://graphml.graphdrawing.org/xmlns/1.0/graphml.xsd">
<graph edgedefault="directed">
<node id="99"/>
<node id="bottles"/>
<node id="of"/>
output = ""
bottles = 99
def bottle_str(bottles):
if bottles == 0:
return "No bottles"
elif bottles == 1:
return "1 bottle"
else:
return f"{bottles} bottles"
@bogardpd
bogardpd / routes.rb
Created September 30, 2019 01:11
/config/routes.rb Routing
#/config/routes.rb
get "/flights/flights.gpx" => "flights#show_flight_gpx", as: :show_flight_gpx
get "/flights/flights.kml" => "flights#show_flight_kml", as: :show_flight_kml
@bogardpd
bogardpd / flights_controller.rb
Created September 30, 2019 01:09
/app/flights_controller.rb Controller Actions
#/app/flights_controller.rb
def show_flight_gpx
flights = flyer.flights(current_user)
render xml: FlightsMap.new(flights).gpx
end
def show_flight_kml
flights = flyer.flights(current_user)
render xml: FlightsMap.new(flights).kml
@bogardpd
bogardpd / map.rb
Created September 30, 2019 01:08
/app/classes/map.rb Completing the kml method
#/app/classes/map.rb
def kml
@airport_details ||= airport_details
used_airports = @airport_details.keys
output = %Q(<?xml version="1.0" encoding="UTF-8" ?>).html_safe
output += content_tag(:kml, xmlns: "http://www.opengis.net/kml/2.2") do
content_tag(:Document) do
concat content_tag(:name, map_name)
concat content_tag(:description, map_description)
@bogardpd
bogardpd / map.rb
Created September 30, 2019 01:07
/app/classes/map.rb KML LineStrings (Routes)
#/app/classes/map.rb
def kml_routes(routes, name)
return nil unless routes.any?
routes = routes.map{|r| r.sort_by{|x| @airport_details[x][:iata]}}.uniq.sort_by{|y| [@airport_details[y[0]][:iata], @airport_details[y[1]][:iata]]}
return content_tag(:Folder) do
concat content_tag(:name, name)
concat safe_join(routes.map{|r| kml_route(r)})
end
end
@bogardpd
bogardpd / map.rb
Created September 30, 2019 01:07
/app/classes/map.rb KML Points (Airports)
#/app/classes/map.rb
def kml_airports(airports)
airports = airports.sort_by{|a| @airport_details[a][:iata]}
return content_tag(:Folder) do
concat content_tag(:name, "Airports")
concat safe_join(airports.map{|a| kml_airport(a)})
end
end
@bogardpd
bogardpd / map.rb
Created September 30, 2019 01:06
/app/classes/map.rb KML Metadata and Style
#/app/classes/map.rb
def kml_styles
output = content_tag(:Style, id: "airportMarker") do
content_tag(:Icon) do
content_tag(:href, "http://maps.google.com/mapfiles/kml/shapes/placemark_circle.png")
end
end
output += content_tag(:Style, id: "flightPath") do
content_tag(:LineStyle) do