Created
December 27, 2013 15:03
-
-
Save nus/8148166 to your computer and use it in GitHub Desktop.
野々市市の地形データをKMLデータで出力
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# coding: utf-8 | |
import re | |
import time | |
import requests | |
from lxml import etree | |
KML_FILE_NAME = 'nonoichi_city.kml' | |
KML_HEADER_TEMPLATE = ( | |
"<?xml version='1.0' encoding='UTF-8'?>" | |
"<kml xmlns='http://www.opengis.net/kml/2.2'>" | |
'<Document>' | |
'<name>町内会</name>\n') | |
KML_PLACEMARK_HEADER = ( | |
u'<Placemark>' | |
'<styleUrl>#my_style</styleUrl>' | |
'<name>%(name)s</name>' | |
'<Polygon>' | |
'<outerBoundaryIs>' | |
'<LinearRing>' | |
'<tessellate>0</tessellate>' | |
'<coordinates>\n') | |
KML_PLACEMARK_FOOTER = ( | |
'</coordinates>' | |
'</LinearRing>' | |
'</outerBoundaryIs>' | |
'</Polygon>' | |
'</Placemark>\n') | |
KML_STYLE_TEMPLATE = ( | |
"<Style id='my_style'>" | |
'<LineStyle>' | |
'<color>ff000000</color>' | |
'<width>4</width>' | |
'</LineStyle>' | |
'<PolyStyle>' | |
'<color>4C000000</color>' | |
'<fill>1</fill>' | |
'<outline>1</outline>' | |
'</PolyStyle>' | |
'</Style>') | |
KML_FOOTER_TEMPLATE = ( | |
'</Document>' | |
'</kml>\n') | |
def take_xml_data(url, encoding='euc-jp'): | |
r = requests.get(url) | |
r.encoding = encoding | |
text = r.text.encode(encoding) | |
parser = etree.XMLParser(recover=True, encoding=encoding) | |
root = etree.fromstring(text) | |
return root | |
def take_contour_data_as_xml(cid, url='http://gm.nono1.jp/modules/map7/gxmlhttp-xml6.php?cid='): | |
return take_xml_data(url + cid) | |
def main(): | |
# 町内会のインデックスデータを取得 | |
index_url = 'http://gm.nono1.jp/modules/map7/gxmlhttp-xml6.php?cid=2' | |
root = take_xml_data(index_url) | |
neighborhood_association_index = {} | |
for elem in root.xpath('//markers/cats'): | |
attr = elem.attrib | |
name = attr['c_title'] | |
name = re.sub(u'--=|--=|--', '', name) | |
cid = attr['c_cid'] | |
neighborhood_association_index[name] = cid | |
kml_file = open(KML_FILE_NAME, 'w') | |
kml_file.write(KML_HEADER_TEMPLATE) | |
for (name, cid) in neighborhood_association_index.items(): | |
print name | |
MUST_SKIP = name == u'町内会' | |
if MUST_SKIP: | |
continue | |
out = KML_PLACEMARK_HEADER % {'name': name} | |
out = out.encode('utf-8') | |
kml_file.write(out) | |
contour_xml_data = take_contour_data_as_xml(cid) | |
for marker in contour_xml_data.xpath('//markers/marker'): | |
attr = marker.attrib | |
lng = attr['lng'] | |
lat = attr['lat'] | |
kml_file.write('%s,%s,0 ' % (lng, lat)) | |
kml_file.write(KML_PLACEMARK_FOOTER) | |
# サーバに付加を書けないためにスリープ | |
time.sleep(0.5) | |
kml_file.write(KML_STYLE_TEMPLATE) | |
kml_file.write(KML_FOOTER_TEMPLATE) | |
if __name__ == '__main__': | |
main() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment