Permalink
Browse files

d3-plugin simplify is deprecated. patch to use topojson -s. add vote …

…related data
  • Loading branch information...
1 parent b3e7cf0 commit 6f2e7927ddb07d98d8a3585c89f53562e4a69005 @zbryikt zbryikt committed Mar 23, 2013
View
@@ -1,4 +1,3 @@
tmp
node_modules
*.swp
-tw*.json
View
@@ -1,7 +1,7 @@
-all:: twCounty1982.json twTown1982.json twCounty2010.json
+all:: twVillage1982.topo.json twTown1982.topo.json twCounty2010.topo.json twVote1982.topo.json
clean::
- rm -f tw*.json
+ rm -f tw*.geo.json tw*.topo.json
rm -rf tmpdir
tmpdir:
@@ -16,6 +16,10 @@ tmpdir/tw-village.rar: tmpdir
tmpdir/tw-county.rar: tmpdir
curl -o $@ http://www.iot.gov.tw/public/Attachment/7101816594871.rar
+tmpdir/TWN_VILLAGE.shp: tmpdir/tw-village.rar tmpdir
+ (cd tmpdir && unrar x ../$<)
+ touch $@
+
tmpdir/TWN_TOWN.shp: tmpdir/tw-town.rar tmpdir
(cd tmpdir && unrar x ../$<)
touch $@
@@ -24,25 +28,21 @@ tmpdir/TWN_COUNTY.shp: tmpdir/tw-county.rar tmpdir
(cd tmpdir && unrar x ../$<)
touch $@
-twCounty1982raw.json: tmpdir/TWN_COUNTY.shp
- ogr2ogr -f geojson $@ $<
-
-twCounty1982.json: twCounty1982raw.json
- ./node_modules/.bin/lsc bin/tw-counties.ls --simplify 0.0005 $< > $@
+# original command: ogr2ogr -f geojson $@ $<
+twCounty2010.geo.json: tmpdir/TWN_COUNTY.shp
+ ./bin/shp2geojson.py $< $@
-twCounty2010.json: twCounty1982raw.json
- ./node_modules/.bin/lsc bin/tw-counties.ls --2010 --simplify 0.0005 $< > $@
+twTown1982.geo.json: tmpdir/TWN_TOWN.shp
+ ./bin/shp2geojson.py $< $@
-twTown1982raw.json: tmpdir/TWN_TOWN.shp
- ogr2ogr -f geojson $@ $<
+twVillage1982.geo.json: tmpdir/TWN_VILLAGE.shp
+ ./bin/shp2geojson.py $< $@
-twTown1982.json: twTown1982raw.json
- ./node_modules/.bin/lsc bin/tw-counties.ls --town --simplify 0.0005 $< > $@
+twVote1982.geo.json: tmpdir/TWN_VILLAGE.shp
+ ./vote/shp2geojson-vote.py $< $@
-twTown2010.json: twTown1982raw.json
- ./node_modules/.bin/lsc bin/tw-counties.ls --town --2010 --simplify 0.0005 $< > $@
+.SUFFIXES: .geojson .topojson
-.SUFFIXES: .json .topojson
+%.topo.json: %.geo.json
+ ./node_modules/.bin/topojson -p -s 0.00000001 $< > $@
-%.topojson: %.json
- ./node_modules/.bin/topojson --properties name $< > $@
View
@@ -0,0 +1,59 @@
+#!/usr/bin/python
+# -*- coding: utf-8 -*- #coding=utf-8
+import os,sys,re,glob,json
+import gdal,ogr,json
+
+if len(sys.argv)<3:
+ print("usage: shp2votetopojson.py infile.shp out.topojson")
+ sys.exit(-1)
+
+infile,outfile = sys.argv[1], sys.argv[2]
+patch_county = {u"台南縣":u"台南市",u"台北縣":u"新北市",u"台中縣":u"台中市",u"高雄縣":u"高雄市"}
+
+def makename(prop):
+ ahash,ret = {}, ""
+ for item in prop: ahash[item[0]] = item[1]
+ ret = ahash["COUNTYNAME"]
+ if "TOWNNAME" in ahash: ret += "/%s"%ahash["TOWNNAME"]
+ if "VILLAGENAM" in ahash: ret += "/%s"%ahash["VILLAGENAM"]
+ return ret
+
+def patch(name, value, direct=False):
+ if name=="COUNTYNAME": return (patch_county[value] if value in patch_county else value)
+ if not direct: return value
+ if name=="TOWNNAME": return re.sub(".$", u"", value, flags=re.UNICODE)
+ if name=="VILLAGENAM": return re.sub(".$", u"", value, flags=re.UNICODE)
+
+shape = ogr.Open(infile)
+layer = shape.GetLayerByIndex(0)
+
+features = {}
+for f in layer:
+ g = f.GetGeometryRef()
+ if not g: continue
+ prop = []
+ direct = False
+ for i in xrange(0,f.GetFieldCount()):
+ name = f.GetFieldDefnRef(i).GetName().decode("utf-8")
+ value = f.GetFieldAsString(i).decode("utf-8")
+ if value in patch_county: direct = True
+ value = patch(name, value, direct)
+ prop += [[name, value]]
+ name = makename(prop)
+ prop += [["name", name]]
+ if not name in features: features[name] = [prop,None]
+ if features[name][1]: features[name][1] = features[name][1].Union(g)
+ else: features[name][1] = g.Clone()
+
+ftext = []
+for f in features:
+ prop,g = features[f]
+ ftext += ['{"properties":{%s},"type":"Feature","geometry":%s}'%(
+ (",".join([('"%s":"%s"'%(p[0],p[1])) for p in prop])).encode("utf-8"),
+ g.ExportToJson()
+ )]
+
+if os.path.exists(outfile): os.remove(outfile)
+fp = open(outfile, "w")
+fp.write('{"type": "FeatureCollection", "features": [%s]}'%(",".join(ftext) ))
+fp.close()
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
View

Large diffs are not rendered by default.

Oops, something went wrong.
View

Large diffs are not rendered by default.

Oops, something went wrong.
View

Large diffs are not rendered by default.

Oops, something went wrong.
View

Large diffs are not rendered by default.

Oops, something went wrong.
File renamed without changes.
File renamed without changes.
File renamed without changes.
@@ -13,9 +13,9 @@
patch_county = {"台南縣":"台南市","台北縣":"新北市","台中縣":"台中市","高雄縣": "高雄市"}
# query: m3166["TPQ"] = "新北市"
-m3166 = json.load(open("3166-2-tw.json","r"))
+m3166 = json.load(open("vote/3166-2-tw.json","r"))
-raw_sec = json.load(open("ly-section.json","r"))
+raw_sec = json.load(open("vote/ly-section.json","r"))
# query: section[u"新北市-雙溪區-上林里"] = ["TPQ","12"]
section = {}
for c in raw_sec.keys():
@@ -40,8 +40,8 @@
print("warning: 找不到 '%s' 的選區對應"%key)
continue
c,n = section[key]
- if key==u"屏東縣-滿州鄉-響林村" or key==u"高雄市-六龜區-建山里":
- print("%s: %s"%(key,n))
+ #if key==u"屏東縣-滿州鄉-響林村" or key==u"高雄市-六龜區-建山里":
+ # print("%s: %s"%(key,n))
if not c in sec_geom: sec_geom[c] = {}
if not n in sec_geom[c]: sec_geom[c][n] = []
sec_geom[c][n] += [[g,key]]
File renamed without changes.
File renamed without changes.
View
@@ -1,38 +0,0 @@
-#!/usr/bin/python
-# -*- coding: utf-8 -*-
-#coding=utf-8
-import os,sys,re,glob,json
-import gdal,ogr,json
-
-patch_county = {"台南縣":"台南市","台北縣":"新北市","台中縣":"台中市","高雄縣": "高雄市"}
-# query: m3166["TPQ"] = "新北市"
-m3166 = json.load(open("3166-2-tw.json","r"))
-raw_sec = json.load(open("ly-section.json","r"))
-# query: section[u"新北市-雙溪區-上林里"] = ["TPQ","12"]
-section = {}
-for c in raw_sec.keys():
- for n in raw_sec[c].keys():
- for item in raw_sec[c][n]: section["%s-%s"%(m3166[c],item)] = [c,n]
-
-shape = ogr.Open("shp/TWN_VILLAGE.shp")
-layer = shape.GetLayerByIndex(0)
-
-sec_geom = {}
-features = []
-for f in layer:
- g = f.GetGeometryRef()
- if not g: continue
- county,town,village = f.GetFieldAsString(2), f.GetFieldAsString(3), f.GetFieldAsString(4)
- if county in patch_county:
- county,town,village = patch_county[county],re.sub("...$","",town),re.sub("", "", village)
- key = re.sub("","","%s-%s-%s"%(county,town,village)).decode("utf8")
- features += ['{"properties":{"name":"%s"},"type":"Feature","geometry":%s}'%(
- key.encode("utf8"),
- g.ExportToJson()
- )]
-
-outfn = "web/villiage.json"
-if os.path.exists(outfn): os.remove(outfn)
-outfile = open(outfn, "w")
-outfile.write('{"type": "FeatureCollection", "features": [%s]}'%(",".join(features) ))
-outfile.close()

0 comments on commit 6f2e792

Please sign in to comment.