# Indian Latitude Longitude values

I am in need of around 10000 and odd Indian latitude and longitude values for my openlayers project. I have to load them in a postgis db. so a sql file or atleast text file would be better than a link.

This is a nice problem for OGR's python interface. This is my attempt, using world.shp. Here's my code snippet:

``````from osgeo import ogr # Import ogr
import random # Random number generators etc.

g = ogr.Open ("world.shp") # Open the shapefile with all the world's countries
# Loop over the different features until we find INDIA.
for feat in g.GetLayer(0):
if feat.GetFieldAsString("NAME").find ("INDIA") >= 0:
break # We found India, break out of the loop.
# feat now holds the feature of INDIA

geom = feat.GetGeometryRef() # Get the geometry of Indina
( lon_min, lon_max, lat_min, lat_max ) = geom.GetEnvelope() # Now get the envelope

n_points = 0 # A counter
india_lon = [] # A list where we'll store longitudes within India
india_lat = [] # A list where we'll store latitudes within India
while n_points < 1000: # Until we reach our required number of points...
# Draw random longitudes and latitudes within the envelope
r_lon = random.random()*(lon_max - lon_min ) + lon_min
r_lat = random.random()*(lat_max - lat_min ) + lat_min
# Create a point geometry
pt = ogr.Geometry( ogr.wkbPoint )
pt.SetPoint_2D( 0, r_lon, r_lat )
# Is this geometry within india?
if pt.Within ( geom ):
# yes! Store the latitude&longitude and update counter
india_lon.append ( r_lon )
india_lat.append ( r_lat )
n_points = n_points + 1
# Save to a CSV file
fp=open("india.txt", 'w' )
for (lon, lat) in zip (india_lon, india_lat):
fp.write ("%f,%f\n" % ( lon, lat ) )

fp.close()
``````

And here's how the result looks like:

Not the most efficient way of dealing with this, but nice to learn :)

• Very nice. Regarding efficiency, you aren't too far off. If you replace OGR with Shapely's prepared geometry of India, the result is very impressive. 10k points within India are created in 1.125 sec (vs. nearly 2 min for OGR), a 105x speed-up! Commented Jun 6, 2011 at 8:20
• Mmmm... True, I guess it does help a lot to prepare such a complex (multi)polygon. Can you use prepared geometries directly from OGR? I don't seem to find any references to it, I guess the GEOS prepared geometries stuff is not exposed to OGR...
– Jose
Commented Jun 6, 2011 at 12:40
• Yeah, OGR generally exposes only a small part of GEOS. Shapely exposes much more of GEOS, and in my opinion is much more "pythonic". You definitely need OGR to read the Shapefile, but after that I use Shapely for more sophisticated GEOS work. Commented Jun 6, 2011 at 12:49
• Here is my version of the script: pastebin.com/k57u01ay feel free to use it in any way you want Commented Jun 6, 2011 at 13:19
• I haven't had the chance to review the PostGIS functions yet (they are fresh). Start by adding the `RandomPoint` and `RandomPointMulti` functions into PostGIS (by running the SQL), then try something like `SELECT RandomPoint(india_poly.geom) FROM india_poly`, where `india_poly` is a 1-row geometry table with a polygon of India. Commented Jun 3, 2011 at 2:40