Are there any webserveices/APIs that can take a country name and return a random coordinates (not including the sea/ocean sections) inside that country?

  • 2
    GPS coordinates are those collected by a GPS receiver. If you're looking for random point locations within a country, please edit the question to request that. – Vince Sep 23 '15 at 13:18
  • So the coordinates tag should be removed? – Arya Sep 23 '15 at 13:24
  • 2
    There's nothing GPS-specific about "coordinates". "geocoding" did not seem to apply. – Vince Sep 23 '15 at 13:41
  • 2
    use the overpass api (openstreetmap) here is an example of finding a placename within a bounding box overpass-turbo.eu/s/bCD (use the data tab to see the raw output after running) – Mapperz Sep 23 '15 at 13:45
  • @Mapperz thanks for the link, but I'm trying to use this from my Java program. Is there an API that I can call from my program? Even a paid option would be usefull for me – Arya Oct 2 '15 at 4:34

It is not a web-service or an API, however, it can be used to solve the issue.

For solving this task, shapefiles were downloaded from EfrainMaps. (It can be any other website that includes shapefiles covering the whole world, e.g. Thematic Mapping API or Natural Earth | Admin 0 – Countries).

Using the following Python code it is possible to take a country name and return a random point with its coordinates (not including the sea/ocean sections) inside that country.

Before running the code keep in mind the attributes of that particular shapefile, thus the country name that you put into the function should match its corresponding feature's attribute in the shapefile.

Getting a random point inside a country based on its name.
In this solution, the shapefile with countries was used.

# imports
import re
import random
import shapefile
from shapely.geometry import shape, Point

# function that takes a shapefile location and a country name as inputs
def random_point_in_country(shp_location, country_name):
    shapes = shapefile.Reader(shp_location) # reading shapefile with pyshp library
    country = [s for s in shapes.records() if country_name in s][0] # getting feature(s) that match the country name 
    country_id = int(re.findall(r'\d+', str(country))[0]) # getting feature(s)'s id of that match

    shapeRecs = shapes.shapeRecords()
    feature = shapeRecs[country_id].shape.__geo_interface__

    shp_geom = shape(feature)

    minx, miny, maxx, maxy = shp_geom.bounds
    while True:
        p = Point(random.uniform(minx, maxx), random.uniform(miny, maxy))
        if shp_geom.contains(p):
            return p.x, p.y

random_point_in_country("D:/World_Countries/World_Countries.shp", "Ukraine")

The output will look like

(47.545113016917696, 33.4408633374681)

and the tuple is indeed located in Ukraine.

p.s. by the way it works also offline


Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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