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?
-
2GPS 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.– VinceCommented Sep 23, 2015 at 13:18
-
So the coordinates tag should be removed?– AryaCommented Sep 23, 2015 at 13:24
-
2There's nothing GPS-specific about "coordinates". "geocoding" did not seem to apply.– VinceCommented Sep 23, 2015 at 13:41
-
2use 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 ♦Commented Sep 23, 2015 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– AryaCommented Oct 2, 2015 at 4:34
1 Answer
It is not a web service or an API, however, it can be used to solve the issue.
Using the following Python scripts 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.
Solution 1 : with a URL
It is primarily based on the data in GeoJSON format provided under the preferred URL, I have chosen this one: https://github.com/johan/world.geo.json.
"""
Getting a random point inside a country based on its name.
In this solution, the URL with countries as a GeoJSON was used.
"""
# imports
import json, random, requests
from shapely.geometry import shape, Point
def random_point_in_country(country_name: str) -> tuple:
"""
Getting coordinates of a random point inside a country chosen by its name
Parameters:
==========
:param country_name: a country name
Returns:
==========
:return: a tuple with coordinates y and x
"""
url: str = "https://raw.githubusercontent.com/johan/world.geo.json/master/countries.geo.json"
try:
response = requests.get(url)
if response.status_code == 200:
data = json.loads(response.text)
country = [country for country in data["features"] if country["properties"]["name"] in country_name][0]
feature = shape(country["geometry"])
minx, miny, maxx, maxy = feature.bounds
while True:
pnt = Point(random.uniform(minx, maxx), random.uniform(miny, maxy))
if feature.contains(pnt):
return float(pnt.y), float(pnt.x)
except Exception as e:
return 0, 0
print(random_point_in_country("Ukraine"))
The output will look like:
(48.17489680780856, 30.76394835467746)
Solution 2 : with a data file
It uses a geospatial file stored on the machine.
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. ⚠️ However, keep in mind this online thread: http://switchfromshapefile.org/
"""
Getting a random point inside a country based on its name.
In this solution, the shapefile with countries was used.
"""
# imports
import re, random, shapefile
from os.path import normpath
from shapely.geometry import shape, Point
def random_point_in_country(shp_location: str, country_name: str) -> tuple:
"""
Getting coordinates of a random point inside a country chosen by its name
Parameters:
==========
:param shp_location: a path to the shapefile
:param country_namea country name
Returns:
==========
:return: a tuple with coordinates x and y
"""
try:
shapes = shapefile.Reader(normpath(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:
pnt = Point(random.uniform(minx, maxx), random.uniform(miny, maxy))
if feature.contains(pnt):
return float(pnt.x), float(pnt.y)
except Exception as e:
return 0, 0
print(random_point_in_country("D:/World_Countries/World_Countries.shp", "Ukraine"))
The output will look like
(47.545113016917696, 33.4408633374681)
References: