OGR examples

Create a point


In [1]:
from osgeo import ogr
point = ogr.Geometry(ogr.wkbPoint)

In [2]:
point.AddPoint(1198054.34, 648493.09)
print point.ExportToWkt()


POINT (1198054.34 648493.09 0)

In [3]:
point.GetGeometryName()


Out[3]:
'POINT'

Buffer a point geometry


In [4]:
bufferDistance = 500
bufpoint = point.Buffer(bufferDistance)
print "%s buffered by %d is %s" % (point.ExportToWkt(), bufferDistance, bufpoint.ExportToWkt())


POINT (1198054.34 648493.09 0) buffered by 500 is POLYGON ((1198554.34 648493.09,1198553.654767377302051 648466.922021878533997,1198551.600947684142739 648440.825768366106786,1198548.18417029757984 648414.87276747985743,1198543.41380036692135 648389.134154591127299,1198537.302913144696504 648363.680477448739111,1198529.868258147733286 648338.581502812448889,1198521.130213248776272 648313.906025227275677,1198511.112728821346536 648289.721678462112322,1198499.843262094305828 648266.094750130199827,1198487.352701892377809 648243.09,1198473.675283972872421 648220.770482492400333,1198458.848497187485918 648199.197373853763565,1198442.912980728549883 648178.429804475046694,1198425.912412738893181 648158.524696820531972,1198407.893390593351796 648139.536609406699426,1198388.905303179519251 648121.517587261274457,1198369.000195524888113 648104.51701927150134,1198348.232626146404073 648088.581502812448889,1198326.659517507534474 648073.754716027295217,1198304.34 648060.077298107789829,1198281.335249869851395 648047.586737905745395,1198257.708321538055316 648036.317271178704686,1198233.523974772775546 648026.299786751391366,1198208.848497187485918 648017.561741852434352,1198183.749522551428527 648010.127086855471134,1198158.295845408923924 648004.016199633013457,1198132.557232520310208 647999.245829702354968,1198106.604231633944437 647995.829052315792069,1198080.507978121517226 647993.775232622632757,1198054.34 647993.09,1198028.172021878650412 647993.775232622632757,1198002.075768366223201 647995.829052315792069,1197976.12276747985743 647999.245829702354968,1197950.384154591243714 648004.016199633013457,1197924.930477448739111 648010.127086855471134,1197899.83150281268172 648017.561741852434352,1197875.156025227392092 648026.299786751391366,1197850.971678462112322 648036.317271178704686,1197827.344750130316243 648047.586737905745395,1197804.34 648060.077298107789829,1197782.020482492633164 648073.754716027295217,1197760.447373853763565 648088.581502812448889,1197739.679804475279525 648104.51701927150134,1197719.774696820648387 648121.517587261274457,1197700.786609406815842 648139.536609406699426,1197682.767587261274457 648158.524696820531972,1197665.767019271617755 648178.429804475046694,1197649.83150281268172 648199.197373853763565,1197635.004716027295217 648220.770482492400333,1197621.327298107789829 648243.09,1197608.83673790586181 648266.094750130199827,1197597.567271178821102 648289.721678462112322,1197587.549786751391366 648313.906025227275677,1197578.811741852434352 648338.581502812448889,1197571.377086855471134 648363.680477448739111,1197565.266199633246288 648389.134154591127299,1197560.495829702587798 648414.87276747985743,1197557.079052316024899 648440.825768366106786,1197555.025232622865587 648466.922021878533997,1197554.34 648493.09,1197555.025232622865587 648519.257978121400811,1197557.079052316024899 648545.354231633828022,1197560.495829702587798 648571.307232520077378,1197565.266199633246288 648597.045845408807509,1197571.377086855471134 648622.499522551195696,1197578.811741852434352 648647.598497187485918,1197587.549786751391366 648672.27397477265913,1197597.567271178821102 648696.458321537822485,1197608.83673790586181 648720.08524986973498,1197621.327298107789829 648743.09,1197635.004716027295217 648765.409517507534474,1197649.83150281268172 648786.982626146171242,1197665.767019271617755 648807.750195524888113,1197682.767587261274457 648827.655303179402836,1197700.786609406815842 648846.643390593235381,1197719.774696820648387 648864.66241273866035,1197739.679804475046694 648881.662980728433467,1197760.447373853763565 648897.598497187485918,1197782.020482492633164 648912.425283972639591,1197804.34 648926.102701892144978,1197827.344750130316243 648938.593262094189413,1197850.971678462112322 648949.862728821230121,1197875.156025227392092 648959.880213248543441,1197899.83150281268172 648968.618258147500455,1197924.930477448739111 648976.052913144463673,1197950.384154591243714 648982.16380036692135,1197976.12276747985743 648986.93417029757984,1198002.075768366223201 648990.350947684142739,1198028.172021878650412 648992.404767377302051,1198054.34 648993.09,1198080.507978121517226 648992.404767377302051,1198106.604231633944437 648990.350947684142739,1198132.557232520310208 648986.93417029757984,1198158.295845408923924 648982.16380036692135,1198183.749522551428527 648976.052913144463673,1198208.848497187485918 648968.618258147500455,1198233.523974772775546 648959.880213248543441,1198257.708321538055316 648949.862728821230121,1198281.335249869851395 648938.593262094189413,1198304.34 648926.102701892144978,1198326.659517507534474 648912.425283972639591,1198348.232626146404073 648897.598497187485918,1198369.000195524888113 648881.662980728433467,1198388.905303179519251 648864.66241273866035,1198407.893390593351796 648846.643390593235381,1198425.912412738893181 648827.655303179402836,1198442.912980728549883 648807.750195524888113,1198458.848497187485918 648786.982626146171242,1198473.675283972872421 648765.409517507534474,1198487.352701892377809 648743.09,1198499.843262094305828 648720.08524986973498,1198511.112728821346536 648696.458321537822485,1198521.130213248776272 648672.27397477265913,1198529.868258147733286 648647.598497187485918,1198537.302913144696504 648622.499522551195696,1198543.41380036692135 648597.045845408807509,1198548.18417029757984 648571.307232520077378,1198551.600947684142739 648545.354231633828022,1198553.654767377302051 648519.257978121400811,1198554.34 648493.09))

In [5]:
centroid = bufpoint.Centroid()
print centroid
# This must be equal to the input point for the buffer


POINT (1198054.34 648493.089999999850988)

Create a line


In [6]:
from osgeo import ogr
line = ogr.Geometry(ogr.wkbLineString)
line.AddPoint(1116651.439379124, 637392.6969887456)
line.AddPoint(1188804.0108498496, 652655.7409537067)
line.AddPoint(1226730.3625203592, 634155.0816022386)
line.AddPoint(1281307.30760719, 636467.6640211721)
print line.ExportToWkt()


LINESTRING (1116651.439379123970866 637392.696988745592535 0,1188804.010849849553779 652655.740953706670552 0,1226730.362520359223709 634155.08160223858431 0,1281307.307607189984992 636467.66402117209509 0)

In [7]:
print "Geometry has %i points" % (line.GetPointCount())


Geometry has 4 points

In [8]:
for i in range(0, line.GetPointCount()):
    # GetPoint returns a tuple not a Geometry
    pt = line.GetPoint(i)
    print "%i). POINT (%d %d)" %(i, pt[0], pt[1])


0). POINT (1116651 637392)
1). POINT (1188804 652655)
2). POINT (1226730 634155)
3). POINT (1281307 636467)

In [9]:
print "Length = %d" % line.Length()


Length = 170573

In [10]:
line.GetGeometryName()


Out[10]:
'LINESTRING'

Create a polygon


In [11]:
from osgeo import ogr

# Create ring
ring = ogr.Geometry(ogr.wkbLinearRing)
ring.AddPoint(1179091.1646903288, 712782.8838459781)
ring.AddPoint(1161053.0218226474, 667456.2684348812)
ring.AddPoint(1214704.933941905, 641092.8288590391)
ring.AddPoint(1228580.428455506, 682719.3123998424)
ring.AddPoint(1218405.0658121984, 721108.1805541387)
ring.AddPoint(1179091.1646903288, 712782.8838459781)

# Create polygon
poly = ogr.Geometry(ogr.wkbPolygon)
poly.AddGeometry(ring)

print poly.ExportToWkt()


POLYGON ((1179091.164690328761935 712782.883845978067257 0,1161053.021822647424415 667456.268434881232679 0,1214704.933941904921085 641092.828859039116651 0,1228580.428455505985767 682719.312399842427112 0,1218405.065812198445201 721108.180554138729349 0,1179091.164690328761935 712782.883845978067257 0))

In [12]:
# Get Envelope returns a tuple (minX, maxX, minY, maxY)
env = poly.GetEnvelope()
print "minX: %d, minY: %d, maxX: %d, maxY: %d" %(env[0],env[2],env[1],env[3])


minX: 1161053, minY: 641092, maxX: 1228580, maxY: 721108

In [13]:
print "Area = %d" % poly.GetArea()


Area = 3489273550

In [14]:
poly.GetGeometryName()


Out[14]:
'POLYGON'

Buffer a polygon geometry


In [15]:
bufferDistance = 500
bufpoly = poly.Buffer(bufferDistance)
print "%s buffered by %d is %s" % (poly.ExportToWkt(), bufferDistance, bufpoly.ExportToWkt())


POLYGON ((1179091.164690328761935 712782.883845978067257 0,1161053.021822647424415 667456.268434881232679 0,1214704.933941904921085 641092.828859039116651 0,1228580.428455505985767 682719.312399842427112 0,1218405.065812198445201 721108.180554138729349 0,1179091.164690328761935 712782.883845978067257 0)) buffered by 500 is POLYGON ((1178987.579469376476482 713272.036278252722695,1218301.480591246159747 721597.332986413384788,1218327.255845648935065 721602.089056905708276,1218353.244916843017563 721605.487898549181409,1218379.376388529781252 721607.520171545096673,1218405.578453103313223 721608.180291338008828,1218431.779108972288668 721607.466443960904144,1218457.906358417356387 721605.380591020570137,1218483.888405434554443 721601.928464306984097,1218509.653853027150035 721597.119550042669289,1218535.13189939991571 721590.967062815325335,1218560.252532517071813 721583.487909264513291,1218584.946722492575645 721574.702641624142416,1218609.146611277479678 721564.635401245439425,1218632.785699131898582 721553.313852258492261,1218655.79902736027725 721540.769105552928522,1218678.123356814263389 721527.035633287159726,1218699.697341670282185 721512.151174160884693,1218720.4616980033461 721496.156629712088034,1218740.359366695629433 721479.095951921888627,1218759.335670231375843 721461.016022437135689,1218777.338462946936488 721441.966523743118159,1218794.318274323828518 721421.999802639009431,1218810.228444931097329 721401.170726392185315,1218825.025254641659558 721379.536531966528855,1218838.668042772216722 721357.156668739160523,1218851.119319817284122 721334.092635137145407,1218862.34487046697177 721310.407809643540531,1218872.313847629120573 721286.167276638094336,1218880.998857194790617 721261.437647548737004,1218888.376033315202221 721236.286877808277495,1229063.738676622742787 682847.418723511975259,1229069.826498327543959 682821.730931771569885,1229074.550026639830321 682795.757628210238181,1229077.896093794843182 682769.571218525059521,1229079.855371971381828 682743.244702488300391,1229080.422399295726791 682716.851470446330495,1229079.59559506806545 682690.465098729589954,1229077.377264167414978 682664.159144543809816,1229073.773590628057718 682638.006940915249288,1229068.79462039959617 682612.081392260151915,1229062.45423334161751 682586.454771149205044,1229054.77010453119874 682561.198516834061593,1215179.275590930134058 640934.714976030751131,1215170.472761062905192 640910.418311490793712,1215160.426350902067497 640886.608914935728535,1215149.163197130197659 640863.35038771352265,1215136.713386682327837 640840.704859650577419,1215123.110176374670118 640818.732823086786084,1215108.389904066454619 640797.492971283732913,1215092.591891593066975 640777.042041639215313,1215075.758339724969119 640757.43466412613634,1215057.934215439250693 640738.723215361125767,1215039.167131800204515 640720.957678692531772,1215019.507220772095025 640704.185510681243613,1214999.006999302422628 640688.451514331391081,1214977.721229036571458 640673.797719409340061,1214955.706770032411441 640660.263270170777105,1214933.02242887346074 640647.884320796001703,1214909.728801579913124 640636.693938811891712,1214885.888111740350723 640626.722016760264523,1214861.564044295111671 640617.995192345930263,1214836.82157541741617 640610.536777281085961,1214811.726798944408074 640604.366695012664422,1214786.346749821444973 640599.501427501789294,1214760.74922503484413 640595.953971195966005,1214735.00260250759311 640593.733802311937325,1214709.175658442545682 640592.846851521986537,1214683.337383603677154 640593.295488112256862,1214657.556799022480845 640595.078513652668335,1214631.902771623339504 640598.191165199154057,1214606.443830263335258 640602.625128016108647,1214581.24798267101869 640608.368557787965983,1214556.382533780764788 640615.40611225774046,1214531.913905941881239 640623.718992211157456,1214507.907461487688124 640633.28499169414863,1214484.427328134421259 640644.078557330649346,1160832.515208876924589 667007.518133172765374,1160809.717457682825625 667019.458423561416566,1160787.564276192570105 667032.555924999644049,1160766.114353322656825 667046.775939167710021,1160745.424514894606546 667062.080793949542567,1160725.549573090160266 667078.429943234426901,1160706.542181240627542 667095.780074333189987,1160688.452694336185232 667114.085222723777406,1160671.329035623930395 667133.296893821796402,1160655.216569648357108 667153.364191453787498,1160640.157982069300488 667174.233952693175524,1160626.193166578421369 667195.850888701272197,1160613.359119211789221 667218.157731199869886,1160601.689840337727219 667241.095384188462049,1160591.216244583250955 667264.603080503526144,1160581.966078932862729 667288.618542804033495,1160573.963849221356213 667313.078148559550755,1160567.230755211552605 667337.917098600184545,1160561.784634431358427 667363.069588785408996,1160557.639914918225259 667388.468984333798289,1160554.80757699534297 667414.047996354638599,1160553.295124183176085 667439.738860111217946,1160553.106563320150599 667465.47351454582531,1160554.242393947206438 667491.183782589039765,1160556.699606986250728 667516.801551776588894,1160560.471692709252238 667542.258954695309512,1160565.548657986568287 667567.488548779278062,1160571.917052759323269 667592.423494981136173,1160579.56000567227602 667616.997734843986109,1160588.457268770318478 667641.14616550586652,1178626.600136451655999 712967.761576602701098,1178636.786520990310237 712991.547401081887074,1178648.189780931686983 713014.774401293601841,1178660.779377073980868 713037.38037274137605,1178674.521593075245619 713059.304774112883024,1178689.379625748377293 713080.488889416796155,1178705.313683625310659 713100.875985229969956,1178722.281093522440642 713120.41146263666451,1178740.236414823913947 713139.043003450147808,1178759.131561177317053 713156.720710326568224,1178778.915929273935035 713173.397240395774134,1178799.536534369457513 713189.027932050754316,1178820.938152184709907 713203.570924556348473,1178843.063466800609604 713216.987270156969316,1178865.853224157355726 713229.241038383101113,1178889.246390743879601 713240.299412277177908,1178913.180317051475868 713250.132776280865073,1178937.590905356686562 713258.714795548818074,1178962.412781382212415 713266.022486476460472,1178987.579469376476482 713272.036278252722695))

In [16]:
bufpoly.GetGeometryName()


Out[16]:
'POLYGON'

In [17]:
centroid = bufpoly.Centroid()
print centroid


POINT (1198366.724720017286018 682817.35505709622521)

Intersection


In [18]:
intersection = poly.Intersection(bufpoly)

print intersection.ExportToWkt()


POLYGON ((1179091.164690328761935 712782.883845978067257 0,1218405.065812198445201 721108.180554138729349 0,1228580.428455505985767 682719.312399842427112 0,1214704.933941904921085 641092.828859039116651 0,1161053.021822647424415 667456.268434881232679 0,1179091.164690328761935 712782.883845978067257 0))

Union


In [19]:
union = poly.Union(bufpoly)
print union.ExportToWkt()


POLYGON ((1178987.579469376476482 713272.036278252722695 0,1218301.480591246159747 721597.332986413384788 0,1218327.255845648935065 721602.089056905708276 0,1218353.244916843017563 721605.487898549181409 0,1218379.376388529781252 721607.520171545096673 0,1218405.578453103313223 721608.180291338008828 0,1218431.779108972288668 721607.466443960904144 0,1218457.906358417356387 721605.380591020570137 0,1218483.888405434554443 721601.928464306984097 0,1218509.653853027150035 721597.119550042669289 0,1218535.13189939991571 721590.967062815325335 0,1218560.252532517071813 721583.487909264513291 0,1218584.946722492575645 721574.702641624142416 0,1218609.146611277479678 721564.635401245439425 0,1218632.785699131898582 721553.313852258492261 0,1218655.79902736027725 721540.769105552928522 0,1218678.123356814263389 721527.035633287159726 0,1218699.697341670282185 721512.151174160884693 0,1218720.4616980033461 721496.156629712088034 0,1218740.359366695629433 721479.095951921888627 0,1218759.335670231375843 721461.016022437135689 0,1218777.338462946936488 721441.966523743118159 0,1218794.318274323828518 721421.999802639009431 0,1218810.228444931097329 721401.170726392185315 0,1218825.025254641659558 721379.536531966528855 0,1218838.668042772216722 721357.156668739160523 0,1218851.119319817284122 721334.092635137145407 0,1218862.34487046697177 721310.407809643540531 0,1218872.313847629120573 721286.167276638094336 0,1218880.998857194790617 721261.437647548737004 0,1218888.376033315202221 721236.286877808277495 0,1229063.738676622742787 682847.418723511975259 0,1229069.826498327543959 682821.730931771569885 0,1229074.550026639830321 682795.757628210238181 0,1229077.896093794843182 682769.571218525059521 0,1229079.855371971381828 682743.244702488300391 0,1229080.422399295726791 682716.851470446330495 0,1229079.59559506806545 682690.465098729589954 0,1229077.377264167414978 682664.159144543809816 0,1229073.773590628057718 682638.006940915249288 0,1229068.79462039959617 682612.081392260151915 0,1229062.45423334161751 682586.454771149205044 0,1229054.77010453119874 682561.198516834061593 0,1215179.275590930134058 640934.714976030751131 0,1215170.472761062905192 640910.418311490793712 0,1215160.426350902067497 640886.608914935728535 0,1215149.163197130197659 640863.35038771352265 0,1215136.713386682327837 640840.704859650577419 0,1215123.110176374670118 640818.732823086786084 0,1215108.389904066454619 640797.492971283732913 0,1215092.591891593066975 640777.042041639215313 0,1215075.758339724969119 640757.43466412613634 0,1215057.934215439250693 640738.723215361125767 0,1215039.167131800204515 640720.957678692531772 0,1215019.507220772095025 640704.185510681243613 0,1214999.006999302422628 640688.451514331391081 0,1214977.721229036571458 640673.797719409340061 0,1214955.706770032411441 640660.263270170777105 0,1214933.02242887346074 640647.884320796001703 0,1214909.728801579913124 640636.693938811891712 0,1214885.888111740350723 640626.722016760264523 0,1214861.564044295111671 640617.995192345930263 0,1214836.82157541741617 640610.536777281085961 0,1214811.726798944408074 640604.366695012664422 0,1214786.346749821444973 640599.501427501789294 0,1214760.74922503484413 640595.953971195966005 0,1214735.00260250759311 640593.733802311937325 0,1214709.175658442545682 640592.846851521986537 0,1214683.337383603677154 640593.295488112256862 0,1214657.556799022480845 640595.078513652668335 0,1214631.902771623339504 640598.191165199154057 0,1214606.443830263335258 640602.625128016108647 0,1214581.24798267101869 640608.368557787965983 0,1214556.382533780764788 640615.40611225774046 0,1214531.913905941881239 640623.718992211157456 0,1214507.907461487688124 640633.28499169414863 0,1214484.427328134421259 640644.078557330649346 0,1160832.515208876924589 667007.518133172765374 0,1160809.717457682825625 667019.458423561416566 0,1160787.564276192570105 667032.555924999644049 0,1160766.114353322656825 667046.775939167710021 0,1160745.424514894606546 667062.080793949542567 0,1160725.549573090160266 667078.429943234426901 0,1160706.542181240627542 667095.780074333189987 0,1160688.452694336185232 667114.085222723777406 0,1160671.329035623930395 667133.296893821796402 0,1160655.216569648357108 667153.364191453787498 0,1160640.157982069300488 667174.233952693175524 0,1160626.193166578421369 667195.850888701272197 0,1160613.359119211789221 667218.157731199869886 0,1160601.689840337727219 667241.095384188462049 0,1160591.216244583250955 667264.603080503526144 0,1160581.966078932862729 667288.618542804033495 0,1160573.963849221356213 667313.078148559550755 0,1160567.230755211552605 667337.917098600184545 0,1160561.784634431358427 667363.069588785408996 0,1160557.639914918225259 667388.468984333798289 0,1160554.80757699534297 667414.047996354638599 0,1160553.295124183176085 667439.738860111217946 0,1160553.106563320150599 667465.47351454582531 0,1160554.242393947206438 667491.183782589039765 0,1160556.699606986250728 667516.801551776588894 0,1160560.471692709252238 667542.258954695309512 0,1160565.548657986568287 667567.488548779278062 0,1160571.917052759323269 667592.423494981136173 0,1160579.56000567227602 667616.997734843986109 0,1160588.457268770318478 667641.14616550586652 0,1178626.600136451655999 712967.761576602701098 0,1178636.786520990310237 712991.547401081887074 0,1178648.189780931686983 713014.774401293601841 0,1178660.779377073980868 713037.38037274137605 0,1178674.521593075245619 713059.304774112883024 0,1178689.379625748377293 713080.488889416796155 0,1178705.313683625310659 713100.875985229969956 0,1178722.281093522440642 713120.41146263666451 0,1178740.236414823913947 713139.043003450147808 0,1178759.131561177317053 713156.720710326568224 0,1178778.915929273935035 713173.397240395774134 0,1178799.536534369457513 713189.027932050754316 0,1178820.938152184709907 713203.570924556348473 0,1178843.063466800609604 713216.987270156969316 0,1178865.853224157355726 713229.241038383101113 0,1178889.246390743879601 713240.299412277177908 0,1178913.180317051475868 713250.132776280865073 0,1178937.590905356686562 713258.714795548818074 0,1178962.412781382212415 713266.022486476460472 0,1178987.579469376476482 713272.036278252722695 0))

To GeoJson


In [23]:
# Create the output Driver
outDriver = ogr.GetDriverByName('GeoJSON')

# Create the output GeoJSON
outDataSource = outDriver.CreateDataSource('./test.geojson')
outLayer = outDataSource.CreateLayer('./test.geojson', geom_type=ogr.wkbPolygon )

# Get the output Layer's Feature Definition
featureDefn = outLayer.GetLayerDefn()

# create a new feature
outFeature = ogr.Feature(featureDefn)

# Set new geometry
outFeature.SetGeometry(union)

# Add new feature to output Layer
outLayer.CreateFeature(outFeature)

# destroy the feature
outFeature.Destroy

# Close DataSources
outDataSource.Destroy()

Export to GeoJson


In [24]:
geojson = poly.ExportToJson()
print geojson


{ "type": "Polygon", "coordinates": [ [ [ 1179091.164690328761935, 712782.883845978067257, 0.0 ], [ 1161053.021822647424415, 667456.268434881232679, 0.0 ], [ 1214704.933941904921085, 641092.828859039116651, 0.0 ], [ 1228580.428455505985767, 682719.312399842427112, 0.0 ], [ 1218405.065812198445201, 721108.180554138729349, 0.0 ], [ 1179091.164690328761935, 712782.883845978067257, 0.0 ] ] ] }

Export to KML


In [25]:
kml = poly.ExportToKML()
print kml


<Polygon><outerBoundaryIs><LinearRing><coordinates>91.164690328761935,712782.883845978067257,0 53.021822647424415,667456.268434881232679,0 64.933941904921085,641092.828859039116651,0 -99.571544494014233,682719.312399842427112,0 165.065812198445201,721108.180554138729349,0 91.164690328761935,712782.883845978067257,0</coordinates></LinearRing></outerBoundaryIs></Polygon>

In [ ]: