Geographic Information Systems Stack Exchange is a question and answer site for cartographers, geographers and GIS professionals. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

I have tweets with coordinates. I'm trying to bucket the tweets by country using the coordinate information. I know that I can achieve this using some hosted web services but all of these have API limits that I am sure to run into.

UPDATE:

I started writing this code learning from another resource about osgeo library.

import osgeo.ogr
shapefile = osgeo.ogr.Open("geodata/worldborders/TM_WORLD_BORDERS-0.3.shp")
layer = shapefile.GetLayer(0)

for i in range(layer.GetFeatureCount()):
    feature = layer.GetFeature(i)
    countryCode = feature.GetField("ISO3")
    countryName = feature.GetField("NAME")
    print countryCode, countryName

    geometry = feature.GetGeometryRef()

The geometry variable has a member function called Contains that can take another Geometry and checks whether the other is contained inside itself.

I have a coordinate value from Twitter data in the following format: [45.29680725, -75.92681408]. However an example on osgeo showcases creation of a point like this:

point = osgeo.ogr.Geometry(osgeo.ogr.wkbPoint)
point.SetPoint(0, 474595, 4429281)

How do I convert my coordinates into the above required format? If I do manage to convert, is this the right approach of mapping twitter coordinates to countries?

share|improve this question
    
Removed answer once you made edit since you prefer to work with shapefile rather than GeoJSON. – evv_gis Feb 27 '14 at 22:58
    
Could you paste the link to the GeoJSON resource you provided in the answer? I could explore loading that data using shapely and using streamhacker.com/2010/03/23/python-point-in-polygon-shapely. Am I correct in saying that the coordinates in the data file your shared are in the same format as those from Twitter as I showed above? – Prashanth Ellina Feb 27 '14 at 23:02
    
github.com/che0/countries solves the problem. I am unable to add an answer as I don't have enough reputation. – Prashanth Ellina Feb 28 '14 at 0:15
1  
Here is the link, for anyone else who may be interested: 04478727369367745762.googlegroups.com/attach/cd1638345088524‌​c/… The coordinates are Lat/Lng. – evv_gis Feb 28 '14 at 13:55
up vote 4 down vote accepted

This achieves finding country given coordinates. https://github.com/che0/countries

It uses a shapefile containing world borders loaded using python bindings for gdal.

Example:

 import countries
 cc = countries.CountryChecker('TM_WORLD_BORDERS-0.3.shp')
 print cc.getCountry(countries.Point(49.7821, 3.5708)).iso

The world borders shapefile can be obtained from http://thematicmapping.org/downloads/world_borders.php.

share|improve this answer

Given that you only need to find what country the tweets are in I guess the VMAP0 data is sufficient.

As for Python libraries I would recommend looking at Shapely, which supports the Shapely library, a turorial on point-in-polygon can be found here. As for reading in the country polygons from the vmap0-data (which i presume are polygons in shapefiles), you should look at ogr.

share|improve this answer
    
Thanks. I've updated the question. Can you check it out please? – Prashanth Ellina Feb 27 '14 at 22:56

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.