Take the 2-minute tour ×
Geographic Information Systems Stack Exchange is a question and answer site for cartographers, geographers and GIS professionals. It's 100% free, no registration required.

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.

share|improve this question
add comment

2 Answers 2

up vote 4 down vote accepted

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' )
fp.write( "Longitude, Latitude\n" )# Header
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: One thousand random points over India

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

share|improve this answer
1  
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! –  Mike T Jun 6 '11 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 Jun 6 '11 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. –  Mike T Jun 6 '11 at 12:49
1  
Here is my version of the script: pastebin.com/k57u01ay feel free to use it in any way you want –  Mike T Jun 6 '11 at 13:19
add comment

You can generate 10000 random points within a polygon of India. There has been some recent work to do this:


Update I've added a set-returning function to the PostGIS User's wiki, where I've used this question as an example. See that page for details. Although PostGIS also uses a prepared geometry, it isn't as fast as Shapely. My best speed for 10k points is 3023 ms (compared with 1125 ms for Shapely for same problem).

share|improve this answer
    
I am thankful for ur reply. Can you please guide how to execute that function in trac.osgeo.org/postgis/wiki/UserWikiRandomPoint and also how the input be given –  Vikash Talanki Jun 3 '11 at 2:17
    
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. –  Mike T Jun 3 '11 at 2:40
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.