Object-oriented update

parent e039c4ed
......@@ -3,7 +3,7 @@
# Rio de Janeiro provided as a parameter,
# using MongoDB's RJ_Bairros collection.
import sys
import datetime
from gpx import GPX
from pymongo import MongoClient
# District name as parameter
......@@ -13,49 +13,12 @@ if len(sys.argv) < 2:
sys.exit()
district = str(sys.argv[1])
outfile = district.replace(" ", "_").lower() + ".gpx"
# GPX file
gpxfile = district.replace(" ", "_").lower() + ".gpx"
gpx = open(gpxfile, "w")
gpx.write("<?xml version=\"1.0\" encoding=\"UTF-8\" ?>\n")
gpx.write("<gpx version=\"1.1\" " + \
"creator=\"Reficio - http://reficio.cc\" " + \
"xmlns=\"http://www.topografix.com/GPX/1/1\" " + \
"xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\" " + \
"xsi:schemaLocation=\"http://www.topografix.com/GPX/1/1 " + \
"http://www.topografix.com/GPX/1/1/gpx.xsd\">\n")
gpx.write("\t<metadata>\n")
gpx.write("\t\t<name>" + district + "</name>\n")
gpx.write("\t\t<desc>Portal de Dados Geográficos Abertos da Cidade do Rio de Janeiro</desc>\n")
gpx.write("\t\t<author>\n")
gpx.write("\t\t\t<name>Halley Pacheco de Oliveira</name>\n")
gpx.write("\t\t\t<email id=\"reficio\" domain=\"reficio.cc\" />\n")
gpx.write("\t\t\t<link href=\"http://reficio.cc/\">\n")
gpx.write("\t\t\t\t<text>Reficio</text>\n")
gpx.write("\t\t\t</link>\n")
gpx.write("\t\t</author>\n")
gpx.write("\t</metadata>\n")
def trkpt(lat, lon):
gpx.write("\t\t\t<trkpt lat=\"" + lat + "\" lon=\"" + lon + "\"></trkpt>\n")
return
def trkbeg(name):
gpx.write("\t<trk>\n")
gpx.write("\t\t<name>" + name + "</name>\n")
# To calculate the center of the map
minlat = 90
maxlat = -90
minlon = 180
maxlon = -180
def minmaxlatlon(lat, lon):
global maxlat, minlat, maxlon, minlon
maxlat = max(lat,maxlat)
minlat = min(lat,minlat)
maxlon = max(lon,maxlon)
minlon = min(lon,minlon)
# GPX class
gpx = GPX()
gpx.gpxbeg(district, \
"Portal de Dados Geográficos Abertos da Cidade do Rio de Janeiro")
# Connect to reficio database on MongoDB server
conn = MongoClient('localhost', 27017)
......@@ -63,38 +26,28 @@ db = conn.reficio
# District perimeter
for dist in db.PCRJ_Bairros.find({'properties.NOME' : district}):
trkbeg(dist['properties']['NOME'])
gpx.trkbeg(dist['properties']['NOME'])
for geo in dist['geometry']['coordinates']:
if dist['geometry']['type'] == 'Polygon':
gpx.write("\t\t<trkseg>\n")
gpx.trksegbeg()
for point in geo:
trkpt(str(point[1]), str(point[0]))
minmaxlatlon(point[1], point[0])
gpx.trkpt(point[1], point[0])
gpx.minmaxlatlon(point[1], point[0])
gpx.write("\t\t</trkseg>\n")
gpx.trksegend()
else:
for coord in geo:
gpx.write("\t\t<trkseg>\n")
gpx.trksegbeg()
for point in coord:
trkpt(str(point[1]), str(point[0]))
minmaxlatlon(point[1], point[0])
gpx.trkpt(point[1], point[0])
gpx.minmaxlatlon(point[1], point[0])
gpx.write("\t\t</trkseg>\n")
gpx.trksegend()
gpx.write("\t</trk>\n")
gpx.trkend()
gpx.write("</gpx>")
gpx.close()
gpx.gpxend()
gpx.write(outfile)
# WordPress OpenStreetMap Plugin
medlat = (minlat + maxlat)/2
medlon = (minlon + maxlon)/2
d = datetime.date.today()
s = "[osm_map_v3 map_center=\"" + \
str(medlat) + "," + str(medlon) + \
"\" zoom=\"14\" width=\"100%\" height=\"480\" " + \
"map_border=\"thin solid grey\" " + \
"file_list=\"../../../../wp-content/uploads/" + \
'{:04d}'.format(d.year) + "/" + '{:02d}'.format(d.month) + \
"/" + gpxfile + "\" file_color_list=\"blue\"]"
print(s)
\ No newline at end of file
gpx.wposmap(outfile)
\ No newline at end of file
This diff is collapsed.
......@@ -2,59 +2,13 @@
# districts of the city of Rio de Janeiro using the
# MongoDB collections PCRJ_Locais and PCRJ_Bairros.
import bson
import html
import datetime
from gpx import GPX
from pymongo import MongoClient
# GPX file
gpxfile = "pcrj_morros_bairros_rio.gpx"
gpx = open(gpxfile, "w")
gpx.write("<?xml version=\"1.0\" encoding=\"UTF-8\" ?>\n")
gpx.write("<gpx version=\"1.1\" " + \
"creator=\"Reficio - http://reficio.cc\" " + \
"xmlns=\"http://www.topografix.com/GPX/1/1\" " + \
"xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\" " + \
"xsi:schemaLocation=\"http://www.topografix.com/GPX/1/1 " + \
"http://www.topografix.com/GPX/1/1/gpx.xsd\">\n")
gpx.write("\t<metadata>\n")
gpx.write("\t\t<name>Hills and districts of the city of Rio de Janeiro</name>\n")
gpx.write("\t\t<desc>Portal de Dados Geográficos Abertos da Cidade do Rio de Janeiro</desc>\n")
gpx.write("\t\t<author>\n")
gpx.write("\t\t\t<name>Halley Pacheco de Oliveira</name>\n")
gpx.write("\t\t\t<email id=\"reficio\" domain=\"reficio.cc\" />\n")
gpx.write("\t\t\t<link href=\"http://reficio.cc/\">\n")
gpx.write("\t\t\t\t<text>Reficio</text>\n")
gpx.write("\t\t\t</link>\n")
gpx.write("\t\t</author>\n")
gpx.write("\t</metadata>\n")
def wpt(name, desc, lat, lon):
gpx.write("\t<wpt lat=\"" + lat + "\" lon=\"" + lon + "\">\n")
gpx.write("\t\t<name>" + html.escape(name) + "</name>\n")
gpx.write("\t\t<desc>" + html.escape(desc) + "</desc>\n")
gpx.write("\t</wpt>\n")
return
def trkpt(lat, lon):
gpx.write("\t\t\t<trkpt lat=\"" + lat + "\" lon=\"" + lon + "\"></trkpt>\n")
return
def trkbeg(name):
gpx.write("\t<trk>\n")
gpx.write("\t\t<name>" + name + "</name>\n")
# To calculate the center of the map
minlat = 90
maxlat = -90
minlon = 180
maxlon = -180
def minmaxlatlon(lat, lon):
global maxlat, minlat, maxlon, minlon
maxlat = max(lat,maxlat)
minlat = min(lat,minlat)
maxlon = max(lon,maxlon)
minlon = min(lon,minlon)
# GPX class
gpx = GPX()
gpx.gpxbeg("Hills and districts of the city of Rio de Janeiro", \
"Portal de Dados Geográficos Abertos da Cidade do Rio de Janeiro")
# Connect to reficio database on MongoDB server
conn = MongoClient('localhost', 27017)
......@@ -67,43 +21,33 @@ for hill in db.PCRJ_Locais.find( { 'properties.Nome' : regx } ):
desc = hill['properties']['Descricao']
lat = hill['geometry']['coordinates'][1]
lon = hill['geometry']['coordinates'][0]
wpt(name, desc, str(lat), str(lon))
minmaxlatlon(lat, lon)
gpx.wpt(name, desc, lat, lon)
gpx.minmaxlatlon(lat, lon)
# Perimeters of districts
for dist in db.PCRJ_Bairros.find():
trkbeg(dist['properties']['NOME'])
gpx.trkbeg(dist['properties']['NOME'])
for geo in dist['geometry']['coordinates']:
if dist['geometry']['type'] == 'Polygon':
gpx.write("\t\t<trkseg>\n")
gpx.trksegbeg()
for point in geo:
trkpt(str(point[1]), str(point[0]))
minmaxlatlon(point[1], point[0])
gpx.trkpt(point[1], point[0])
gpx.minmaxlatlon(point[1], point[0])
gpx.write("\t\t</trkseg>\n")
gpx.trksegend()
else:
for coord in geo:
gpx.write("\t\t<trkseg>\n")
gpx.trksegbeg()
for point in coord:
trkpt(str(point[1]), str(point[0]))
minmaxlatlon(point[1], point[0])
gpx.trkpt(point[1], point[0])
gpx.minmaxlatlon(point[1], point[0])
gpx.write("\t\t</trkseg>\n")
gpx.trksegend()
gpx.write("\t</trk>\n")
gpx.trkend()
gpx.write("</gpx>")
gpx.close()
gpx.gpxend()
gpx.write()
# WordPress OpenStreetMap Plugin
medlat = (minlat + maxlat)/2
medlon = (minlon + maxlon)/2
d = datetime.date.today()
s = "[osm_map_v3 map_center=\"" + \
str(medlat) + "," + str(medlon) + \
"\" zoom=\"14\" width=\"100%\" height=\"480\" " + \
"map_border=\"thin solid grey\" " + \
"file_list=\"../../../../wp-content/uploads/" + \
'{:04d}'.format(d.year) + "/" + '{:02d}'.format(d.month) + \
"/" + gpxfile + "\" file_color_list=\"blue\"]"
print(s)
\ No newline at end of file
gpx.wposmap()
\ No newline at end of file
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment