I need help related to OpenStreetMap. I'm using python (jupyter notebook) to get data of hospitals in Bali area, Indonesia. Here is my code and query:
import pandas as pd
import requests
import json
overpass_api = "http://overpass-api.de/api/interpreter"
query_hospital = """
[out:json];
{{geocodeArea:'Provinsi Bali'}}->.searchArea;
node[amenity='hospital'](area.searchArea);
out;
"""
response_hospital = requests.get(overpass_api, params={'data':query_hospital})
but when I run the next code,
data_hospital = response_hospital.json()
it returns error JSONDecodeError: Expecting value: line 1 column 1 (char 0)
the query works well in Overpass Turbo but when I put in notebook, it returns error.
I've found the solution. Looks like python can't parse the double curly braces {{ }} in the openstreetmap query. So I modify the query into like this
query_hospital = """
[out:json];
area[name=Bali];
node[amenity='hospital'](area);
out;
"""
or if we use area name in local language
query_hospital = """
[out:json];
area['name:id'='Provinsi Bali'];
node[amenity='hospital'](area);
out;
"""
the query returns same result and now python can parse it.
Related
I'm trying to extract nutritional information using the Nutritionix API/database in Python. I was able to get a successful query and placed it into a pandas dataframe. However, I'm a bit confused though because the resulting json claims that there are several thousand 'hits' for my query but at most 10 are ever returned. For instance, when I query for Garbanzo, the json file says that there are 513 total_hits, but only 10 are actually returned. Does anyone know what is causing this? The code I'm using is below.
import requests
import json
import pandas as pd
from nutritionix import Nutritionix
nix_apikey = ''
nix_appid = ''
nix = Nutritionix(app_id = nix_appid, api_key = nix_apikey)
results = nix.search('Garbanzo').json()
df = pd.json_normalize(results, record_path = ['hits'])
I'm not including the my api_key or app_id for obvious reasons. Here's a link to the Nutritionix API: https://github.com/leetrout/python-nutritionix
Thanks for any suggestions!
I am trying to pull a query from my database and I am receiving this error when trying to run it: Something went wrong format requires a mapping.
I'm using flask in Python and pymysql.
This is my class method that is throwing the error:
#classmethod
def get_dojo(cls, data):
query = 'SELECT * FROM dojos WHERE id = %(id)s;'
result = connectToMySQL('dojos_and_ninjas').query_db(query, data)
return cls(result[0])
I thought it might be the data I am passing through but it looks good to me, and the query runs fine in workbench. I tried restarting MySQL, VS Code, and restarting the pipenv.
The data I am passing is:
#app.route('/dojo/<int:id>')
def dojo_page(id):
dojo_current = Dojo.get_dojo(id)
return render_template('dojo_page.html', dojo = dojo_current)
My page will render and I receive no error when I enter an id in manually instead of calling the data into it.
I figured it out, I needed to add a data dictionary in the route.
#app.route('/dojo/<int:id>')
def dojo_page(id):
data = {
'id': id
}
dojo_current = Dojo.get_dojo(data)
return render_template('dojo_page.html', dojo = dojo_current)
I am new to python and trying to pull a table from a wiki page into a pandas dataframe. I am using the wikipediaapi to retrieve the URL for the site. (Is there a way to pull the table directly using the api instead of pandas?). Also noteworthy I am trying to use the method as described here.
Below is my current code:
#packages
import wikipediaapi as wpa
import pandas as pd
#get page url
wiki_page = wpa.Wikipedia('en')
page_py = wiki_page.page('Python_(programming_language)')
page_url = page_py.fullurl
#print(page_url)
page_tables = pd.read_html(page_url)[1]
#print(page_tables)
sum_table_df = pd.DataFrame(data=page_tables)
print("page_tables type: ", type(page_tables))
print("sum_table_df type: ", type(page_tables))
print(sum_table_df)
Output:
page_tables type: <class 'pandas.core.frame.DataFrame'>
sum_table_df type: <class 'pandas.core.frame.DataFrame'>
My problem is that the output has not printed out as a normal dataframe would (table formatting). Not sure what I have done incorrectly? Red underlines are the headers for each column..
If you are using Jupyter Notebook and you want your data frame to be formatted as table, do not use print() function to print you data frame. Just run the cell with the name of your data frame, like this:
In [1]: sum_table_df
Out[1]: your formatted data frame
I'm trying to get all USA bus stops from OSM using this Biergarten example for Germany but I haven't achieved that.
This is what I've tried so far:
import requests
import json
overpass_url = "http://overpass-api.de/api/interpreter"
overpass_query = """
[out:json];
area["ISO3166-1"="US"][admin_level=2];
(node["highway"="bus_stop"](area);
way["highway"="bus_stop"](area);
rel["highway"="bus_stop"](area);
);
out center;
"""
response = requests.get(overpass_url,
params={'data': overpass_query})
data = response.json()
The result is an empty list. I would appreciate any hints on this problem – thanks!
I am using the following sparql query using sparqlwrapper as follows.
from SPARQLWrapper import SPARQLWrapper, JSON
sparql = SPARQLWrapper("http://live.dbpedia.org/sparql")
sparql.setReturnFormat(JSON)
my_category = 'dbc:Meteorological_concepts'
sparql.setQuery(f" ASK {{ {my_category} skos:broader{{1,3}} dbc:Medicine }} ")
results = sparql.query().convert()
print(results['boolean'])
As mentioned above it works fine with categories that do not have brackets (e.g., dbc:Meteorological_concepts). However, when I enter a category with brackets (i.e my_category = dbc:Elasticity_(physics)) I get the following error.
b"Virtuoso 37000 Error SP030: SPARQL compiler, line 4: syntax error at 'physics' before ')'\n\nSPARQL query:\ndefine sql:big-data-const 0 \n#output-format:application/sparql-results+json\n\n ASK { dbc:Elasticity_(physics) skos:broader{1,3} dbc:Medicine }\n"
CRITICAL: Exiting due to uncaught exception <class 'SPARQLWrapper.SPARQLExceptions.QueryBadFormed'>
Is there a way to resolve this issue.
I am happy to provide more details if needed.
I am rewriting what #StanislavKralin mentioned in the above comment. I always try to use full URL in the SPARQL code, particularly when there is special character in SPARQL query.
from SPARQLWrapper import SPARQLWrapper, JSON
sparql = SPARQLWrapper("http://live.dbpedia.org/sparql")
sparql.setReturnFormat(JSON)
my_category = '<http://dbpedia.org/resource/Category:Elasticity_(physics)>'
sparql.setQuery(f" ASK {{ {my_category} skos:broader{{1,3}} dbc:Medicine }} ")
results = sparql.query().convert()
print(results['boolean'])