Created
April 11, 2013 11:53
-
-
Save RutgerK/5362782 to your computer and use it in GitHub Desktop.
OGR_ExportToJSON2
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
{ | |
"metadata": { | |
"name": "OGR_ExportToJson" | |
}, | |
"nbformat": 3, | |
"nbformat_minor": 0, | |
"worksheets": [ | |
{ | |
"cells": [ | |
{ | |
"cell_type": "code", | |
"collapsed": false, | |
"input": "import ogr\nimport gdal\ngdal.VersionInfo()", | |
"language": "python", | |
"metadata": {}, | |
"outputs": [ | |
{ | |
"output_type": "pyout", | |
"prompt_number": 18, | |
"text": "'1920'" | |
} | |
], | |
"prompt_number": 18 | |
}, | |
{ | |
"cell_type": "code", | |
"collapsed": false, | |
"input": "driver = ogr.GetDriverByName('Memory')\ndatasource = driver.CreateDataSource('out')\nlyr = datasource.CreateLayer('layername', geom_type=ogr.wkbMultiPolygon)", | |
"language": "python", | |
"metadata": {}, | |
"outputs": [], | |
"prompt_number": 2 | |
}, | |
{ | |
"cell_type": "code", | |
"collapsed": false, | |
"input": "namedef = ogr.FieldDefn('Name', ogr.OFTString)\nnamedef.SetWidth(20)\n\nlyr.CreateField(namedef)\n\nft1 = ogr.Feature( lyr.GetLayerDefn() )\nft2 = ft1.Clone()\nft3 = ft1.Clone()\n\nft1.SetField('Name', 'Test1')\nft2.SetField('Name', 'Test2')\nft3.SetField('Name', 'Test3')\n\nlyr.CreateFeature(ft1)\nlyr.CreateFeature(ft2)\nlyr.CreateFeature(ft3)\n", | |
"language": "python", | |
"metadata": {}, | |
"outputs": [ | |
{ | |
"output_type": "pyout", | |
"prompt_number": 3, | |
"text": "0" | |
} | |
], | |
"prompt_number": 3 | |
}, | |
{ | |
"cell_type": "markdown", | |
"metadata": {}, | |
"source": "Note how the first feature (FID=0) is exported without \"id=0\"" | |
}, | |
{ | |
"cell_type": "code", | |
"collapsed": false, | |
"input": "for ft in lyr:\n print ft.ExportToJson()", | |
"language": "python", | |
"metadata": {}, | |
"outputs": [ | |
{ | |
"output_type": "stream", | |
"stream": "stdout", | |
"text": "{\"geometry\": null, \"type\": \"Feature\", \"properties\": {\"Name\": \"Test1\"}}\n{\"geometry\": null, \"type\": \"Feature\", \"properties\": {\"Name\": \"Test2\"}, \"id\": 1}\n{\"geometry\": null, \"type\": \"Feature\", \"properties\": {\"Name\": \"Test3\"}, \"id\": 2}\n" | |
} | |
], | |
"prompt_number": 4 | |
}, | |
{ | |
"cell_type": "code", | |
"collapsed": false, | |
"input": "", | |
"language": "python", | |
"metadata": {}, | |
"outputs": [] | |
} | |
], | |
"metadata": {} | |
} | |
] | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment