Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
# Searching on public nominatim should work even if a key was provided
ox.config(
nominatim_key="NOT_A_KEY"
)
response_json = ox.nominatim_request(params=params,
type="search")
# Test changing the endpoint. It should fail because we didn't provide a valid key
ox.config(
nominatim_endpoint="http://open.mapquestapi.com/nominatim/v1/"
)
with pytest.raises(Exception):
response_json = ox.nominatim_request(params=params,
type="search")
ox.config(log_console=True, log_file=True, use_cache=True,
data_folder='.temp/data', logs_folder='.temp/logs',
imgs_folder='.temp/imgs', cache_folder='.temp/cache')
params = OrderedDict()
params['format'] = "json"
params['address_details'] = 0
params['osm_ids'] = "W68876073"
response_json = ox.nominatim_request(params=params,
type="lookup")
# Invalid nominatim query type
with pytest.raises(ValueError):
response_json = ox.nominatim_request(
params=params,
type="transfer")
# Searching on public nominatim should work even if a key was provided
ox.config(
nominatim_key="NOT_A_KEY"
)
response_json = ox.nominatim_request(params=params,
type="search")
# Test changing the endpoint. It should fail because we didn't provide a valid key
ox.config(
nominatim_endpoint="http://open.mapquestapi.com/nominatim/v1/"
)
with pytest.raises(Exception):
response_json = ox.nominatim_request(params=params,
type="search")
ox.config(log_console=True, log_file=True, use_cache=True,
data_folder='.temp/data', logs_folder='.temp/logs',
imgs_folder='.temp/imgs', cache_folder='.temp/cache')
import matplotlib as mpl
import warnings
mpl.use('Agg') # use agg backend so you don't need a display on travis-ci
# remove the .temp folder if it already exists so we start fresh with tests
import os, shutil
if os.path.exists('.temp'):
shutil.rmtree('.temp')
import osmnx as ox
from networkx.exception import NetworkXNotImplemented
# configure OSMnx
ox.config(log_console=True, log_file=True, use_cache=True,
data_folder='.temp/data', logs_folder='.temp/logs',
imgs_folder='.temp/imgs', cache_folder='.temp/cache')
def test_imports():
# test all of OSMnx's module imports
import ast
import datetime
import geopandas
import hashlib
import io
import json
import logging
import math
import matplotlib.cm
import matplotlib.pyplot
"""Download and load a small open dataset for testing
Run this to create a CSV of buildings geometries.
Then run:
- load_geometries.sh (loading geometries to the database)
- create_buildings.sh (creating empty building records for each geometry)
"""
# -*- coding: utf-8 -*-
import os
import subprocess
import osmnx
# configure logging/caching
osmnx.config(log_console=True, use_cache=True)
# configure the image display
size = 256
# load buildings from about 1.5km² around UCL
point = (51.524498, -0.133874)
dist = 612
gdf = osmnx.buildings_from_point(point=point, distance=dist)
# preview image
gdf_proj = osmnx.project_gdf(gdf, to_crs={'init': 'epsg:3857'})
fig, ax = osmnx.plot_buildings(gdf_proj, bgcolor='#333333', color='w', figsize=(4,4),
save=True, show=False, close=True,
filename='test_buildings_preview', dpi=600)
# save
def test_overpass():
import pytest
# Test changing the endpoint. This should fail because we didn't provide a valid endpoint
ox.config(
overpass_endpoint="http://NOT_A_VALID_ENDPOINT/api/"
)
with pytest.raises(Exception):
G = ox.graph_from_place('Piedmont, California, USA')
ox.config(overpass_endpoint="http://overpass-api.de/api")
def test_nearest_edges():
from pyproj import Proj
# test in closest edge section
sheik_sayed_dubai = [25.09, 25.06, 55.16, 55.11]
location_coordinates = (25.071764, 55.138978)
G = ox.graph_from_bbox(*sheik_sayed_dubai, simplify=False, retain_all=True, network_type='drive')
# Unprojected
ne1 = ox.get_nearest_edges(G, X=[location_coordinates[1], location_coordinates[1]],
Y=[location_coordinates[0], location_coordinates[0]], method='balltree', dist=0.0001)
# Projected
G2 = ox.project_graph(G)
crs = Proj(G2.graph['crs'])
projected_point = crs(location_coordinates[1], location_coordinates[0])
ne2 = ox.get_nearest_edges(G2, X=[projected_point[0], projected_point[0]],
Y=[projected_point[1], projected_point[1]], method='kdtree', dist=10)
assert (ne1 == ne2).all()
def test_nearest_edge():
# test in closest edge section
sheik_sayed_dubai = [25.09, 25.06, 55.16, 55.11]
location_coordinates = (25.071764, 55.138978)
G = ox.graph_from_bbox(*sheik_sayed_dubai, simplify=False, retain_all=True, network_type='drive')
geometry, u, v = ox.get_nearest_edge(G, location_coordinates)
def test_get_network_methods():
from shapely import wkt
# graph from bounding box
north, south, east, west = 37.79, 37.78, -122.41, -122.43
G1 = ox.graph_from_bbox(north, south, east, west, network_type='drive_service')
G1 = ox.graph_from_bbox(north, south, east, west, network_type='drive_service', truncate_by_edge=True)
# graph from point
location_point = (37.791427, -122.410018)
bbox = ox.bbox_from_point(location_point, project_utm=True)
G2 = ox.graph_from_point(location_point, distance=750, distance_type='bbox', network_type='drive')
G3 = ox.graph_from_point(location_point, distance=500, distance_type='network')
# graph from address
G4 = ox.graph_from_address(address='350 5th Ave, New York, NY', distance=1000, distance_type='network',
network_type='bike')
# graph from list of places
places = ['Los Altos, California, USA', {'city': 'Los Altos Hills', 'state': 'California'}, 'Loyola, California']
G5 = ox.graph_from_place(places, network_type='all', clean_periphery=False)
# graph from polygon
polygon = wkt.loads(
'POLYGON ((-122.418083 37.754154, -122.418082 37.766028, -122.410909 37.766028, -122.410908 37.754154, -122.418083 37.754154))')
G6 = ox.graph_from_polygon(polygon, network_type='walk')
# test custom query filter
filtr = ('["area"!~"yes"]'
# graph from list of places
places = ['Los Altos, California, USA', {'city': 'Los Altos Hills', 'state': 'California'}, 'Loyola, California']
G5 = ox.graph_from_place(places, network_type='all', clean_periphery=False)
# graph from polygon
polygon = wkt.loads(
'POLYGON ((-122.418083 37.754154, -122.418082 37.766028, -122.410909 37.766028, -122.410908 37.754154, -122.418083 37.754154))')
G6 = ox.graph_from_polygon(polygon, network_type='walk')
# test custom query filter
filtr = ('["area"!~"yes"]'
'["highway"!~"motor|proposed|construction|abandoned|platform|raceway"]'
'["foot"!~"no"]'
'["service"!~"private"]'
'["access"!~"private"]')
G = ox.graph_from_point(location_point, network_type='walk', custom_filter=filtr)
def get_network(distance = 1000, center = (54.97351, -1.62545)):
network_pickle_filename = "tests/data/test_network_USB_{}.pkl".format(distance)
if os.path.exists(network_pickle_filename):
network = nx.read_gpickle(path = network_pickle_filename)
else:
network = ox.graph_from_point(
center_point = center,
distance = distance, #meters
distance_type='bbox',
network_type="drive_service")
nx.write_gpickle(G = network, path = network_pickle_filename)
return network