Advertisement
Guest User

Untitled

a guest
May 27th, 2016
62
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.46 KB | None | 0 0
  1. {
  2. "cells": [
  3. {
  4. "cell_type": "markdown",
  5. "metadata": {},
  6. "source": [
  7. "### Testing the construction of PySAL weights object from list of shapely objects"
  8. ]
  9. },
  10. {
  11. "cell_type": "markdown",
  12. "metadata": {},
  13. "source": [
  14. "Author: David C. Folch <dfolch@gmail.com>"
  15. ]
  16. },
  17. {
  18. "cell_type": "markdown",
  19. "metadata": {},
  20. "source": [
  21. "Based on Serge Rey's weights_from_geojson.ipynb"
  22. ]
  23. },
  24. {
  25. "cell_type": "code",
  26. "execution_count": 1,
  27. "metadata": {
  28. "collapsed": true
  29. },
  30. "outputs": [],
  31. "source": [
  32. "import pysal as ps\n",
  33. "from shapely.geometry import MultiPoint\n",
  34. "import numpy as np\n",
  35. "from scipy.spatial import Voronoi"
  36. ]
  37. },
  38. {
  39. "cell_type": "code",
  40. "execution_count": 2,
  41. "metadata": {
  42. "collapsed": true
  43. },
  44. "outputs": [],
  45. "source": [
  46. "class PolygonCollection:\n",
  47. " def __init__(self, polygons, bbox=None):\n",
  48. " \"\"\"\n",
  49. "\n",
  50. " Parameters\n",
  51. " ==========\n",
  52. " polygons: dict\n",
  53. " key is polygon Id, value is PySAL Polygon object\n",
  54. " bbox: list (optional)\n",
  55. " [left, lower, right, upper]\n",
  56. "\n",
  57. " Notes\n",
  58. " =====\n",
  59. " bbox is supported in geojson specification at both the feature and feature collection level. However, not all geojson writers generate the bbox at the feature collection level. \n",
  60. " In those cases, the bbox property will be set on initial access.\n",
  61. "\n",
  62. " \"\"\"\n",
  63. " \n",
  64. " self.type=ps.cg.Polygon\n",
  65. " self.n = len(polygons)\n",
  66. " self.polygons = polygons\n",
  67. " if bbox is None:\n",
  68. " self._bbox = None\n",
  69. " else:\n",
  70. " self._bbox = bbox\n",
  71. " \n",
  72. " @property\n",
  73. " def bbox(self):\n",
  74. " bboxes = np.array([self.polygons[p].bbox for p in self.polygons])\n",
  75. " mins = bboxes.min(axis=0)\n",
  76. " maxs = bboxes.max(axis=0)\n",
  77. " self._bbox = [ mins[0], mins[1], maxs[2], maxs[3] ]\n",
  78. " return self._bbox\n",
  79. " \n",
  80. " \n",
  81. " def __getitem__(self, index):\n",
  82. " return self.polygons[index]"
  83. ]
  84. },
  85. {
  86. "cell_type": "markdown",
  87. "metadata": {},
  88. "source": [
  89. "Some code to get finite voronoi polygons (ignores the vertices at infinity, which works for my use case). Basically it's a way to gin up some random connected shapely objects."
  90. ]
  91. },
  92. {
  93. "cell_type": "code",
  94. "execution_count": 3,
  95. "metadata": {
  96. "collapsed": true
  97. },
  98. "outputs": [],
  99. "source": [
  100. "def get_voronoi(points):\n",
  101. " vor = Voronoi(points)\n",
  102. " vor_polys = []\n",
  103. " for region in vor.regions:\n",
  104. " if region:\n",
  105. " if -1 in region:\n",
  106. " region = region[:] # so we don't change the original voronoi output\n",
  107. " region.remove(-1) # there can only be one -1 in the index list\n",
  108. " vor_polys.append(MultiPoint(vor.vertices[region]).convex_hull)\n",
  109. " return vor_polys"
  110. ]
  111. },
  112. {
  113. "cell_type": "code",
  114. "execution_count": 4,
  115. "metadata": {
  116. "collapsed": true
  117. },
  118. "outputs": [],
  119. "source": [
  120. "def shapely2w(polys, wttype):\n",
  121. " '''\n",
  122. " Parameters:\n",
  123. " ----------\n",
  124. " polys: list\n",
  125. " List of shapely polygons\n",
  126. " wttype: string\n",
  127. " \"rook\" or \"queen\"\n",
  128. "\n",
  129. " Returns\n",
  130. " -------\n",
  131. " pysal weights object\n",
  132. "\n",
  133. " '''\n",
  134. " if wttype.upper() == 'QUEEN':\n",
  135. " wttype = 1\n",
  136. " elif wttype.upper() == 'ROOK':\n",
  137. " wttype = 2\n",
  138. " else:\n",
  139. " raise Exception, \"unknown weight type\"\n",
  140. "\n",
  141. " polys_pysal = []\n",
  142. " poly_ids = []\n",
  143. " poly_index = 0\n",
  144. " for feature in polys:\n",
  145. " polys_pysal.append(ps.cg.asShape(feature))\n",
  146. " poly_ids.append(poly_index)\n",
  147. " poly_index +=1\n",
  148. " pcollection = PolygonCollection(dict(zip(poly_ids, polys_pysal)))\n",
  149. " neighbors = ps.weights.Contiguity.ContiguityWeightsPolygons(pcollection, wttype=wttype).w\n",
  150. " return ps.W(neighbors)"
  151. ]
  152. },
  153. {
  154. "cell_type": "code",
  155. "execution_count": 5,
  156. "metadata": {
  157. "collapsed": false
  158. },
  159. "outputs": [],
  160. "source": [
  161. "np.random.seed(999)\n",
  162. "pnts = np.random.random((100, 2))\n",
  163. "# get some random shapely polygons\n",
  164. "vor_polys_shapely = get_voronoi(pnts)\n",
  165. "# build a W object from the polygons\n",
  166. "w = shapely2w(vor_polys_shapely, 'queen')"
  167. ]
  168. },
  169. {
  170. "cell_type": "code",
  171. "execution_count": 6,
  172. "metadata": {
  173. "collapsed": false
  174. },
  175. "outputs": [
  176. {
  177. "data": {
  178. "text/plain": [
  179. "100"
  180. ]
  181. },
  182. "execution_count": 6,
  183. "metadata": {},
  184. "output_type": "execute_result"
  185. }
  186. ],
  187. "source": [
  188. "w.n"
  189. ]
  190. }
  191. ],
  192. "metadata": {
  193. "kernelspec": {
  194. "display_name": "Python 2",
  195. "language": "python",
  196. "name": "python2"
  197. },
  198. "language_info": {
  199. "codemirror_mode": {
  200. "name": "ipython",
  201. "version": 2
  202. },
  203. "file_extension": ".py",
  204. "mimetype": "text/x-python",
  205. "name": "python",
  206. "nbconvert_exporter": "python",
  207. "pygments_lexer": "ipython2",
  208. "version": "2.7.6"
  209. }
  210. },
  211. "nbformat": 4,
  212. "nbformat_minor": 0
  213. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement