Skip to content

Instantly share code, notes, and snippets.

@nishadhka
Created June 3, 2017 07:44
Show Gist options
  • Save nishadhka/5cd4a749d2389634a20d40223aa95a64 to your computer and use it in GitHub Desktop.
Save nishadhka/5cd4a749d2389634a20d40223aa95a64 to your computer and use it in GitHub Desktop.
AccessingtheBasemapObject.ipynb
Display the source blob
Display the rendered blob
Raw
{
"cells": [
{
"metadata": {
"editable": true,
"deletable": true
},
"cell_type": "markdown",
"source": "In a for loop situtation Basemap object cannot be querried by simple string passing. Objects are special data structure of python and it has to be querried by inbuilt classes such ```getattr``` based on [this](https://stackoverflow.com/questions/2612610/how-to-access-object-attribute-given-string-corresponding-to-name-of-that-attrib) for example below code compline there is no attribute named gridName in state2map"
},
{
"metadata": {
"collapsed": true,
"trusted": true,
"editable": true,
"deletable": true
},
"cell_type": "code",
"source": "for gridName in gridNames[0:1]:\n fig = plt.figure()\n statepa = fig.add_subplot(111)\n state2map = Basemap(epsg=24374,ellps = 'WGS84',llcrnrlon=stateshp3.minx[0]-0.2,llcrnrlat=stateshp3.miny[0]-0.2,urcrnrlon=stateshp3.maxx[0]+0.2,urcrnrlat=stateshp3.maxy[0]+0.2,lat_ts=0,resolution='i',suppress_ticks=True, ax=statepa)\n state2map.readshapefile(gridName,gridName,color='none',zorder=2)\n df_map1 = pd.DataFrame({'geometry': [Polygon(xy) for xy in state2map.gridName)]})",
"execution_count": null,
"outputs": []
},
{
"metadata": {
"editable": true,
"deletable": true
},
"cell_type": "markdown",
"source": "Instead"
},
{
"metadata": {
"collapsed": true,
"trusted": true,
"editable": true,
"deletable": true
},
"cell_type": "code",
"source": "for gridName in gridNames[0:1]:\n fig = plt.figure()\n statepa = fig.add_subplot(111)\n state2map = Basemap(epsg=24374,ellps = 'WGS84',llcrnrlon=stateshp3.minx[0]-0.2,llcrnrlat=stateshp3.miny[0]-0.2,urcrnrlon=stateshp3.maxx[0]+0.2,urcrnrlat=stateshp3.maxy[0]+0.2,lat_ts=0,resolution='i',suppress_ticks=True, ax=statepa)\n state2map.readshapefile(gridName,gridName,color='none',zorder=2)\n df_map1 = pd.DataFrame({'geometry': [Polygon(xy) for xy in getattr(state2map, gridName)]})",
"execution_count": null,
"outputs": []
},
{
"metadata": {
"editable": true,
"deletable": true
},
"cell_type": "markdown",
"source": "Doesn't complain and passes the attribute named as string gridName, it is good to know about the object in python by printing the compnents in it following [this](https://stackoverflow.com/questions/192109/is-there-a-function-in-python-to-print-all-the-current-properties-and-values-of) as follows to get only the attribute name in a object"
},
{
"metadata": {
"collapsed": false,
"trusted": true,
"editable": true,
"deletable": true
},
"cell_type": "code",
"source": "def dump(obj):\n for attr in dir(obj):\n print \"obj.%s\" % (attr)\n \ndump(state2map) ",
"execution_count": null,
"outputs": []
},
{
"metadata": {
"editable": true,
"deletable": true
},
"cell_type": "markdown",
"source": "to get the value too"
},
{
"metadata": {
"collapsed": true,
"trusted": true,
"editable": true,
"deletable": true
},
"cell_type": "code",
"source": "def dump(obj):\n for attr in dir(obj):\n print \"obj.%s = %s\" % (attr, getattr(obj, attr))",
"execution_count": null,
"outputs": []
}
],
"metadata": {
"kernelspec": {
"name": "python2",
"display_name": "Python 2",
"language": "python"
},
"latex_envs": {
"eqNumInitial": 1,
"eqLabelWithNumbers": true,
"current_citInitial": 1,
"cite_by": "apalike",
"bibliofile": "biblio.bib",
"LaTeX_envs_menu_present": true,
"labels_anchors": false,
"latex_user_defs": false,
"user_envs_cfg": false,
"report_style_numbering": false,
"hotkeys": {
"equation": "Ctrl-E",
"itemize": "Ctrl-I"
}
},
"language_info": {
"mimetype": "text/x-python",
"nbconvert_exporter": "python",
"name": "python",
"pygments_lexer": "ipython2",
"version": "2.7.12",
"file_extension": ".py",
"codemirror_mode": {
"version": 2,
"name": "ipython"
}
},
"gist": {
"id": "88c870083a621572cd7e4ecdd9a5a10b",
"data": {
"description": "AccessingtheBasemapObject.ipynb",
"public": true
}
},
"toc": {
"threshold": 4,
"number_sections": true,
"toc_cell": true,
"toc_window_display": true,
"toc_section_display": "block",
"sideBar": true,
"navigate_menu": true,
"moveMenuLeft": true,
"colors": {
"hover_highlight": "#DAA520",
"selected_highlight": "#FFD700",
"running_highlight": "#FF0000"
},
"nav_menu": {
"width": "252px",
"height": "31px"
}
},
"_draft": {
"nbviewer_url": "https://gist.github.com/88c870083a621572cd7e4ecdd9a5a10b"
}
},
"nbformat": 4,
"nbformat_minor": 2
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment