Saturday 26 December 2015

geojson - How to plot geo-data using matplotlib/python


I am trying to plot a polygone on python, using different libararies, but no one of these worked with me. I tried vincent ,Shapely , .. and now matplotlib. I don't wanna use softwares like qgis


I used this example: http://geographika.co.uk/visualising-geojson-in-15-minutes


from matplotlib import pyplot
from descartes import PolygonPatch
import math
import urllib2
import simplejson


data = []
with open('file.json') as f:
for line in f:
data.append(json.loads(line))


def plotFeature(coordlist, myplot):
#create a polygon geojson-like feature
poly = {"type": "Polygon", "coordinates": coordlist}

patch = PolygonPatch(poly, fc='#6699cc', ec='#6699cc', alpha=0.5, zorder=2)
#plot it on the graph
myplot.add_patch(patch)


for coordlist in data['features'][0]['geometry']['coordinates']:
plotFeature(coordlist, myplot)

fig = pyplot.figure(1, figsize=(10, 4), dpi=180)
ax = fig.add_subplot(121)

ax.savefig('myplot.png')

External file "file.json"


var states = [{
"type": "Feature",
"properties": {"party": "Republican"},
"geometry":

{
"type": "Polygon",

"coordinates": [
[
[
-84.32281494140625,
34.9895035675793
],
[
-84.29122924804688,
35.21981940793435
],

[
-84.24041748046875,
35.25459097465022
],
[
-84.22531127929688,
35.266925688950074
],
[
-84.20745849609375,

35.26580442886754
],
[
-84.19921875,
35.24674063355999
],
[
-84.16213989257812,
35.24113278166642
],

[
-84.12368774414062,
35.24898366572645
],
[
-84.09072875976562,
35.24898366572645
],
[
-84.08798217773438,

35.264683153268116
],
[
-84.04266357421875,
35.27701633139884
],
[
-84.03030395507812,
35.291589484566124
],

[
-84.0234375,
35.306160014550784
],
[
-84.03305053710936,
35.32745068492882
],
[
-84.03579711914062,

35.34313496028189
],
[
-84.03579711914062,
35.348735749472546
],
[
-84.01657104492188,
35.35545618392078
],

[
-84.01107788085938,
35.37337460834958
],
[
-84.00970458984374,
35.39128905521763
],
[
-84.01931762695312,

35.41479572901859
],
[
-84.00283813476562,
35.429344044107154
],
[
-83.93692016601562,
35.47409160773029
],

[
-83.91220092773438,
35.47632833265728
],
[
-83.88885498046875,
35.504282143299655
],
[
-83.88473510742186,

35.516578738902936
],
[
-83.8751220703125,
35.52104976129943
],
[
-83.85314941406249,
35.52104976129943
],

[
-83.82843017578125,
35.52104976129943
],
[
-83.8092041015625,
35.53446133418443
],
[
-83.80233764648438,

35.54116627999813
],
[
-83.76800537109374,
35.56239491058853
],
[
-83.7432861328125,
35.56239491058853
],

[
-83.71994018554688,
35.56239491058853
],
[
-83.67050170898438,
35.569097520776054
],
[
-83.6334228515625,

35.570214567965984
],
[
-83.61007690429688,
35.576916524038616
],
[
-83.59634399414061,
35.574682600980914
],

[
-83.5894775390625,
35.55904339525896
],
[
-83.55239868164062,
35.56574628576276
],
[
-83.49746704101562,

35.563512051219696
],
[
-83.47000122070312,
35.586968406786475
],
[
-83.4466552734375,
35.60818490437746
],

[
-83.37936401367188,
35.63609277863135
],
[
-83.35739135742188,
35.65618041632016
],
[
-83.32305908203124,

35.66622234103479
],
[
-83.3148193359375,
35.65394870599763
],
[
-83.29971313476561,
35.660643649881614
],

[
-83.28598022460938,
35.67180064238771
],
[
-83.26126098632811,
35.6907639509368
],
[
-83.25714111328125,

35.69968630125201
],
[
-83.25576782226562,
35.715298012125295
],
[
-83.23516845703125,
35.72310272092263
],

[
-83.19808959960936,
35.72756221127198
],
[
-83.16238403320312,
35.753199435570316
],
[
-83.15826416015625,

35.76322914549896
],
[
-83.10333251953125,
35.76991491635478
],
[
-83.08685302734375,
35.7843988251953
],

[
-83.0511474609375,
35.787740890986576
],
[
-83.01681518554688,
35.78328477203738
],
[
-83.001708984375,

35.77882840327371
],
[
-82.96737670898438,
35.793310688351724
],
[
-82.94540405273438,
35.820040281161
],

[
-82.9193115234375,
35.85121343450061
],
[
-82.9083251953125,
35.86902116501695
],
[
-82.90557861328125,

35.87792352995116
],
[
-82.91244506835938,
35.92353244718235
],
[
-82.88360595703125,
35.94688293218141
],

[
-82.85614013671875,
35.951329861522666
],
[
-82.8424072265625,
35.94243575255426
],
[
-82.825927734375,

35.92464453144099
],
[
-82.80670166015625,
35.927980690382704
],
[
-82.80532836914062,
35.94243575255426
],

[
-82.77923583984375,
35.97356075349624
],
[
-82.78060913085938,
35.99245209055831
],
[
-82.76138305664062,

36.00356252895066
],
[
-82.69546508789062,
36.04465753921525
],
[
-82.64465332031249,
36.060201412392914
],

[
-82.61306762695312,
36.060201412392914
],
[
-82.60620117187499,
36.033552893400376
],
[
-82.60620117187499,

35.991340960635405
],
[
-82.60620117187499,
35.97911749857497
],
[
-82.5787353515625,
35.96133453736691
],

[
-82.5677490234375,
35.951329861522666
],
[
-82.53067016601562,
35.97244935753683
],
[
-82.46475219726562,

36.006895355244666
],
[
-82.41668701171875,
36.070192281208456
],
[
-82.37960815429686,
36.10126686921446
],

[
-82.35488891601562,
36.117908916563685
],
[
-82.34115600585936,
36.113471382052175
],
[
-82.29583740234375,

36.13343831245866
],
[
-82.26287841796874,
36.13565654678543
],
[
-82.23403930664062,
36.13565654678543
],

[
-82.2216796875,
36.154509006695
],
[
-82.20382690429688,
36.15561783381855
],
[
-82.19009399414062,

36.144528857027744
],
[
-82.15438842773438,
36.15007354140755
],
[
-82.14065551757812,
36.134547437460064
],

[
-82.1337890625,
36.116799556445024
],
[
-82.12142944335938,
36.10570509327921
],
[
-82.08984375,

36.10792411128649
],
[
-82.05276489257811,
36.12678323326429
],
[
-82.03628540039062,
36.12900165569652
],

[
-81.91268920898438,
36.29409768373033
],
[
-81.89071655273438,
36.30959215409138
],
[
-81.86325073242188,

36.33504067209607
],
[
-81.83029174804688,
36.34499652561904
],
[
-81.80145263671875,
36.35605709240176
],

[
-81.77947998046874,
36.34610265300638
],
[
-81.76162719726562,
36.33835943134047
],
[
-81.73690795898438,

36.33835943134047
],
[
-81.71905517578125,
36.33835943134047
],
[
-81.70669555664062,
36.33504067209607
],

[
-81.70669555664062,
36.342784223707234
],
[
-81.72317504882812,
36.357163062654365
],
[
-81.73278808593749,

36.379279167407965
],
[
-81.73690795898438,
36.40028364332352
],
[
-81.73690795898438,
36.41354670392876
],

[
-81.72454833984374,
36.423492513472326
],
[
-81.71768188476562,
36.445589751779174
],
[
-81.69845581054688,

36.47541104282962
],
[
-81.69845581054688,
36.51073994146672
],
[
-81.705322265625,
36.53060536411363
],

[
-81.69158935546875,
36.55929085774001
],
[
-81.68060302734375,
36.56480607840351
],
[
-81.68197631835938,

36.58686302344181
],
[
-81.04202270507812,
36.56370306576917
],
[
-80.74264526367186,
36.561496993252575
],

[
-79.89120483398438,
36.54053616262899
],
[
-78.68408203124999,
36.53943280355122
],
[
-77.88345336914062,

36.54053616262899
],
[
-76.91665649414062,
36.54163950596125
],
[
-76.91665649414062,
36.55046568575947
],

[
-76.31103515625,
36.551568887374
],
[
-75.79605102539062,
36.54936246839778
],
[
-75.6298828125,

36.07574221562703
],
[
-75.4925537109375,
35.82226734114509
],
[
-75.3936767578125,
35.639441068973916
],

[
-75.41015624999999,
35.43829554739668
],
[
-75.43212890625,
35.263561862152095
],
[
-75.487060546875,

35.18727767598896
],
[
-75.5914306640625,
35.17380831799959
],
[
-75.9210205078125,
35.04798673426734
],

[
-76.17919921875,
34.867904962568744
],
[
-76.41540527343749,
34.62868797377061
],
[
-76.4593505859375,

34.57442951865274
],
[
-76.53076171875,
34.53371242139567
],
[
-76.5911865234375,
34.551811369170494
],

[
-76.651611328125,
34.615126683462194
],
[
-76.761474609375,
34.63320791137959
],
[
-77.069091796875,

34.59704151614417
],
[
-77.376708984375,
34.45674800347809
],
[
-77.5909423828125,
34.3207552752374
],

[
-77.8326416015625,
33.97980872872457
],
[
-77.9150390625,
33.80197351806589
],
[
-77.9754638671875,

33.73804486328907
],
[
-78.11279296875,
33.8521697014074
],
[
-78.2830810546875,
33.8521697014074
],

[
-78.4808349609375,
33.815666308702774
],
[
-79.6728515625,
34.8047829195724
],
[
-80.782470703125,

34.836349990763864
],
[
-80.782470703125,
34.91746688928252
],
[
-80.9307861328125,
35.092945313732635
],

[
-81.0516357421875,
35.02999636902566
],
[
-81.0516357421875,
35.05248370662468
],
[
-81.0516357421875,

35.137879119634185
],
[
-82.3150634765625,
35.19625600786368
],
[
-82.3590087890625,
35.19625600786368
],

[
-82.40295410156249,
35.22318504970181
],
[
-82.4688720703125,
35.16931803601131
],
[
-82.6885986328125,

35.1154153142536
],
[
-82.781982421875,
35.06147690849717
],
[
-83.1060791015625,
35.003003395276714
],

[
-83.616943359375,
34.99850370014629
],
[
-84.05639648437499,
34.985003130171066
],
[
-84.22119140625,

34.985003130171066
],
[
-84.32281494140625,
34.9895035675793
]
],
[
[
-75.69030761718749,

35.74205383068037
],
[
-75.5914306640625,
35.74205383068037
],
[
-75.5419921875,
35.585851593232356
],

[
-75.56396484375,
35.32633026307483
],
[
-75.69030761718749,
35.285984736065735
],
[
-75.970458984375,

35.16482750605027
],
[
-76.2066650390625,
34.994003757575776
],
[
-76.300048828125,
35.02999636902566
],

[
-76.409912109375,
35.07946034047981
],
[
-76.5252685546875,
35.10642805736423
],
[
-76.4208984375,

35.25907654252574
],
[
-76.3385009765625,
35.294952147406576
],
[
-76.0858154296875,
35.29943548054543
],

[
-75.948486328125,
35.44277092585766
],
[
-75.8660888671875,
35.53669637839501
],
[
-75.772705078125,

35.567980458012094
],
[
-75.706787109375,
35.634976650677295
],
[
-75.706787109375,
35.74205383068037
],

[
-75.69030761718749,
35.74205383068037
]
]
]
} } ]

But it doesn't seem to work out. I need to plot these coordinates with a map on background. Any example, library, tutorial to recommend ?




No comments:

Post a Comment

arcpy - Changing output name when exporting data driven pages to JPG?

Is there a way to save the output JPG, changing the output file name to the page name, instead of page number? I mean changing the script fo...