Monday, May 23, 2011
PostGIS function for a random point inside a polygon -- REVISED
This is a revised random-point-in-polygon function first discussed in my earlier post. It turns out that in some weird cases (multipolygons consisting of a number of small and disperse polygons) the function takes too many iterations to complete. Below is a code for another function called RandomPointMulti that addresses this problem. RandomPointMulti breaks a multipolygon into polygons, randomly chooses a polygon with the probability proportional to the area of the polygon, and then calls original RandomPoint on that polygons. RandomPoint was also modified: some typos have been fixed, exception is thrown if the max number of iterations has been exceeded, and and max number of iterations can be specified as an argument.
Wednesday, April 13, 2011
PostGIS function for a random point inside a polygon
Check the revised version of this post.
This is a function that I wrote to generate a random point inside a polygon. It works by first generating a random point within the bounding box of the polygon and then checking if the point falls inside of the polygon. The limit is 1000 iterations. This limit is probably enough for most practical cases with the exception for polygons that occupy only a small part of its bounding box. This is my first foray into PostgreSQL functions, please judge accordingly. I did not check how the function performs on the geometries other than polygons.
This is a function that I wrote to generate a random point inside a polygon. It works by first generating a random point within the bounding box of the polygon and then checking if the point falls inside of the polygon. The limit is 1000 iterations. This limit is probably enough for most practical cases with the exception for polygons that occupy only a small part of its bounding box. This is my first foray into PostgreSQL functions, please judge accordingly. I did not check how the function performs on the geometries other than polygons.
Friday, January 7, 2011
Importing GNIS into PostgreSQL - II
This is a continuation of my previous post.
To make GNIS useable with spatial queries you need to add geometry or geography column to the table and create a spatial index. Without the index traversing of a 2-million record table is very slow. This is how to do it assuming that the data from GNIS tables has been imported as described in the previous post:
Thursday, December 2, 2010
Importing GNIS into PostgreSQL
The script below will load GNIS National File into PostgreSQL. You can download National File from here (about 280MB uncompressed, main web site). Place the uncompressed file in some directory and run the following script from psql:
The GNIS file has over two million records so it will take some time to load into PostgreSQL. I hope I guessed column types right.
P.S.: GNIS is the "Geographic Names Information System", a product from USGS.
Tuesday, October 5, 2010
Google has come up with a new service with some neat features. Now you can shorten your URL if you go to http://goo.gl/. So, for example, to get to this blog you go to http://goo.gl/Vqml or to go to my main web site as http://goo.gl/AXx5
It even generates a 2D bar code that you can read with your cell phone camera:
It even generates a 2D bar code that you can read with your cell phone camera:
Monday, August 23, 2010
Wednesday, June 16, 2010
Subscribe to:
Posts (Atom)