米国のすべての郡を表すSVG マップを変更して、コロプレス マップを生成しようとしていました。基本的なアプローチはFlowing Dataによってキャプチャされます。SVG は基本的に単なる XML であるため、このアプローチではBeautifulSoupパーサーを利用します。
path
問題は、パーサーがSVG ファイル内のすべての要素をキャプチャするわけではないということです。次の例では、(3000 以上のパスのうち) 149 のパスのみがキャプチャされました。
#Open SVG file
svg=open(shp_dir+'USA_Counties_with_FIPS_and_names.svg','r').read()
#Parse SVG
soup = BeautifulSoup(svg, selfClosingTags=['defs','sodipodi:namedview'])
#Identify counties
paths = soup.findAll('path')
len(paths)
ただし、物理的な検査と、 ElementTreeメソッドが次のルーチンで 3,143 のパスをキャプチャするという事実から、さらに多くのパスが存在することがわかっています。
#Parse SVG
tree = ET.parse(shp_dir+'USA_Counties_with_FIPS_and_names.svg')
#Capture element
root = tree.getroot()
#Compile list of IDs from file
ids=[]
for child in root:
if 'path' in child.tag:
ids.append(child.attrib['id'])
len(ids)
ElementTree
私は、すべてがめちゃくちゃではない方法でオブジェクトから書き込む方法をまだ理解していません。
#Define style template string
style='font-size:12px;fill-rule:nonzero;stroke:#FFFFFF;stroke-opacity:1;'+\
'stroke-width:0.1;stroke-miterlimit:4;stroke-dasharray:none;'+\
'stroke-linecap:butt;marker-start:none;stroke-linejoin:bevel;fill:'
#For each path...
for child in root:
#...if it is a path....
if 'path' in child.tag:
try:
#...update the style to the new string with a county-specific color...
child.attrib['style']=style+col_map[child.attrib['id']]
except:
#...if it's not a county we have in the ACS, leave it alone
child.attrib['style']=style+'#d0d0d0'+'\n'
#Write modified SVG to disk
tree.write(shp_dir+'mhv_by_cty.svg')
上記の変更/書き込みルーチンは、この怪物をもたらします。
path
私の主な質問はこれです: BeautifulSoup がすべてのタグをキャプチャできなかったのはなぜですか? 第二に、オブジェクトで修正された画像ElementTree
に課外活動がすべて含まれているのはなぜでしょうか? アドバイスをいただければ幸いです。