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?
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] # getting feature(s) that match the country name country_id = int(re.findall(r'\d+', str(country))) # 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
and the tuple is indeed located in Ukraine.
p.s. by the way it works also offline