Warning: "continue" targeting switch is equivalent to "break". Did you mean to use "continue 2"? in /home/calcul9/public_html/wp-content/themes/suffusion/functions/media.php on line 580

Warning: "continue" targeting switch is equivalent to "break". Did you mean to use "continue 2"? in /home/calcul9/public_html/wp-content/themes/suffusion/functions/media.php on line 583

Warning: "continue" targeting switch is equivalent to "break". Did you mean to use "continue 2"? in /home/calcul9/public_html/wp-content/themes/suffusion/functions/media.php on line 586

Warning: "continue" targeting switch is equivalent to "break". Did you mean to use "continue 2"? in /home/calcul9/public_html/wp-content/themes/suffusion/functions/media.php on line 589

Warning: "continue" targeting switch is equivalent to "break". Did you mean to use "continue 2"? in /home/calcul9/public_html/wp-content/themes/suffusion/functions/media.php on line 592

Warning: Cannot modify header information - headers already sent by (output started at /home/calcul9/public_html/wp-content/themes/suffusion/functions/media.php:580) in /home/calcul9/public_html/wp-includes/rest-api/class-wp-rest-server.php on line 1673

Warning: Cannot modify header information - headers already sent by (output started at /home/calcul9/public_html/wp-content/themes/suffusion/functions/media.php:580) in /home/calcul9/public_html/wp-includes/rest-api/class-wp-rest-server.php on line 1673

Warning: Cannot modify header information - headers already sent by (output started at /home/calcul9/public_html/wp-content/themes/suffusion/functions/media.php:580) in /home/calcul9/public_html/wp-includes/rest-api/class-wp-rest-server.php on line 1673

Warning: Cannot modify header information - headers already sent by (output started at /home/calcul9/public_html/wp-content/themes/suffusion/functions/media.php:580) in /home/calcul9/public_html/wp-includes/rest-api/class-wp-rest-server.php on line 1673

Warning: Cannot modify header information - headers already sent by (output started at /home/calcul9/public_html/wp-content/themes/suffusion/functions/media.php:580) in /home/calcul9/public_html/wp-includes/rest-api/class-wp-rest-server.php on line 1673

Warning: Cannot modify header information - headers already sent by (output started at /home/calcul9/public_html/wp-content/themes/suffusion/functions/media.php:580) in /home/calcul9/public_html/wp-includes/rest-api/class-wp-rest-server.php on line 1673

Warning: Cannot modify header information - headers already sent by (output started at /home/calcul9/public_html/wp-content/themes/suffusion/functions/media.php:580) in /home/calcul9/public_html/wp-includes/rest-api/class-wp-rest-server.php on line 1673

Warning: Cannot modify header information - headers already sent by (output started at /home/calcul9/public_html/wp-content/themes/suffusion/functions/media.php:580) in /home/calcul9/public_html/wp-includes/rest-api/class-wp-rest-server.php on line 1673
{"id":688,"date":"2011-01-10T19:22:13","date_gmt":"2011-01-11T01:22:13","guid":{"rendered":"http:\/\/www.calculatinginvestor.com\/"},"modified":"2011-03-12T09:04:52","modified_gmt":"2011-03-12T15:04:52","slug":"creating-kml-files-to-show-country-weights-within-a-fund","status":"publish","type":"page","link":"https:\/\/www.calculatinginvestor.com\/octave-code\/creating-kml-files-to-show-country-weights-within-a-fund\/","title":{"rendered":"Creating KML files to show country weights within a fund"},"content":{"rendered":"

Note: <\/strong>This page contains the Python code used to create the KML files for the “Where in the world is my money? – Version 1.0<\/a>” post. <\/em><\/p>\n

Data:<\/em><\/strong>\u00a0<\/p>\n

The data used for the examples in the\u00a0blog post\u00a0comes from the iShares “Fact Sheet<\/a>” for EFA, and from the Vanguard website<\/a> for VWO.<\/p>\n

The data is transfered into a spreadsheet (example<\/a>) and the spreadsheet is then saved as a CSV file for use by the script.<\/p>\n

Code:<\/strong><\/p>\n

Two Python files are used to generate the KML files.\u00a0 The first file reads the CSV file with the fund data and uses Google’s Geocode API to get the latitude and longitude for each country.\u00a0 The filenames for the input CSV file and the output KML file can be changed as desired. The second file contains functions which the first file calls to generate the KML file. This file is read by the main script using the “import” function.<\/p>\n

Main File:<\/p>\n

\r\nimport urllib\r\nimport libxml2\r\nimport csv\r\nimport time\r\nfrom pykml_gen import *\r\n\r\n#Open csv file\r\ninputfile = csv.reader(open('efacountryweight.csv','rb'),delimiter=',')\r\nfirstrow = inputfile.next()\r\n\r\n# template for URL to grab geocode data\r\ntemplate = "http:\/\/maps.googleapis.com\/maps\/api\/geocode\/xml?address=%s&sensor=false"\r\n\r\nweights = []\r\ncountries = []\r\n\r\nhkml = kml_create('EFACountryWeights.kml')\r\n\r\nfor row in inputfile:\r\n    country = row[0]\r\n    print country\r\n    if country != 'Other' and country != '':\r\n        url = template %(country)\r\n        url = url.replace(" ","+")\r\n        time.sleep(0.5)\r\n        stuff = libxml2.parseDoc(urllib.urlopen(url).read())\r\n        lats = [node.content for node in stuff.xpathEval('\/\/lat')]\r\n        lngs = [node.content for node in stuff.xpathEval('\/\/lng')]\r\n        print lats[0]  # Grab first latitude (center of country)\r\n        print lngs[0]  # Grab first longitude (center of country)\r\n        countries.append(country)\r\n        try:\r\n            percent = round(100*float(row[1]),1)\r\n            #print percent\r\n        except:\r\n            percent = 0\r\n        weights.append(percent)\r\n\r\n        # Create Placemark if weight is greater than 0.1%\r\n        if percent > 0.1:\r\n            #color = 'ffff0000' # Blue\r\n            color = 'ff0000ff' # Red\r\n            size =(percent**0.5)\r\n            description = "Country weight in "+firstrow[0]+" on "+firstrow[1]\r\n            kml_style(hkml, size, country,color)\r\n            kml_placemark(hkml, country, percent, lngs[0], lats[0],description)\r\n\r\nkml_close(hkml)\r\n<\/pre>\n

This next section of code is contains the functions used to build the KML file. This file must be saved as “pykml_gen.py” and it must be stored in the same directory as the main script.<\/p>\n

\r\nfrom __future__ import print_function\r\n\r\ndef kml_create(fname):\r\n    hkml = open(fname, 'w')\r\n    print("""<kml xmlns=\\"http:\/\/www.opengis.net\/kml\/2.2\\">\r\n    <Document> """,file=hkml)\r\n    return hkml\r\n\r\ndef kml_folder_start(hkml,folder):\r\n    print("""<Folder>\r\n    <name>"""+folder+"""<\/name>""", file=hkml)\r\n\r\ndef kml_folder_stop(hkml):\r\n    print("""<\/Folder>""", file=hkml)\r\n\r\ndef kml_close(hkml):\r\n    print("""<\/Document>\r\n<\/kml>""", file=hkml)\r\n    hkml.close()\r\n\r\ndef kml_style(hkml,size,name,color):\r\n\tprint("""<Style id="sn_target_"""+name+"""">\r\n\t\t<IconStyle>\r\n\t\t\t<color>"""+color+"""<\/color>\r\n\t\t\t<scale>"""+str(size)+"""<\/scale>\r\n\t\t\t<Icon>\r\n\t\t\t\t<href>http:\/\/maps.google.com\/mapfiles\/kml\/shapes\/shaded_dot.png<\/href>\r\n\t\t\t<\/Icon>\r\n\t\t<\/IconStyle>\r\n\t\t<LabelStyle>\r\n\t\t\t<scale>1.0<\/scale>\r\n\t\t<\/LabelStyle>\r\n\t\t<ListStyle>\r\n\t\t<\/ListStyle>\r\n\t<\/Style>""", file=hkml)\r\n\r\ndef kml_placemark(hkml, name, percent, lon, lat, desc = 'Portfolio Weights'):\r\n    print("""<Placemark>\r\n\t\t<name>"""+name,str(percent)+"%"+"""<\/name>\r\n\t\t<styleUrl>#sn_target_"""+name+"""<\/styleUrl>\r\n                <description>\r\n                 <![CDATA[\r\n                 <p>"""+name,str(percent)+"%"+"""<\/p>\r\n                 <p>"""+desc+"""<\/p>\r\n                 ]]>\r\n                <\/description>\r\n\t\t<Point>\r\n\t\t\t<coordinates>"""+lon+""","""+lat+"""<\/coordinates>\r\n\t\t<\/Point>\r\n\t<\/Placemark>""", file=hkml)\r\n<\/pre>\n","protected":false},"excerpt":{"rendered":"

Note: This page contains the Python code used to create the KML files for the “Where in the world is my money? – Version 1.0” post. Data:\u00a0 The data used for the examples in the\u00a0blog post\u00a0comes from the iShares “Fact Sheet” for EFA, and from the Vanguard website for VWO. The data is transfered into […]<\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"parent":145,"menu_order":0,"comment_status":"open","ping_status":"open","template":"","meta":[],"_links":{"self":[{"href":"https:\/\/www.calculatinginvestor.com\/wp-json\/wp\/v2\/pages\/688"}],"collection":[{"href":"https:\/\/www.calculatinginvestor.com\/wp-json\/wp\/v2\/pages"}],"about":[{"href":"https:\/\/www.calculatinginvestor.com\/wp-json\/wp\/v2\/types\/page"}],"author":[{"embeddable":true,"href":"https:\/\/www.calculatinginvestor.com\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.calculatinginvestor.com\/wp-json\/wp\/v2\/comments?post=688"}],"version-history":[{"count":31,"href":"https:\/\/www.calculatinginvestor.com\/wp-json\/wp\/v2\/pages\/688\/revisions"}],"predecessor-version":[{"id":2032,"href":"https:\/\/www.calculatinginvestor.com\/wp-json\/wp\/v2\/pages\/688\/revisions\/2032"}],"up":[{"embeddable":true,"href":"https:\/\/www.calculatinginvestor.com\/wp-json\/wp\/v2\/pages\/145"}],"wp:attachment":[{"href":"https:\/\/www.calculatinginvestor.com\/wp-json\/wp\/v2\/media?parent=688"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}