-
Notifications
You must be signed in to change notification settings - Fork 4
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Add feature server finding utilities #3
Changes from 1 commit
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
import requests | ||
import pandas as pd | ||
import re | ||
|
||
|
||
class FeatureServerOverview: | ||
def __init__(self): | ||
self.prefix = 'https://mapservices.prorail.nl/' | ||
self.base_url = "https://mapservices.prorail.nl/arcgis/rest/services" | ||
self.df = self.get_all_featureserver_layers() | ||
|
||
def _get_layers_in_featureservers(self, featureserver_url): | ||
featureserver_page = requests.get(featureserver_url) | ||
|
||
featureserver_text = featureserver_page.text | ||
featureservers = re.findall(r'href="/(.+)">(.+)</a> \(\d+\)', featureserver_text) | ||
featureservers = [[f'{self.prefix}{fs}', description] for fs, description in featureservers] | ||
return pd.DataFrame(featureservers, columns=['layer_url', 'description']) | ||
|
||
def get_all_featureserver_layers(self): | ||
all_services = requests.get(self.base_url).text | ||
featureserver_redirects = re.findall(r'href="/(.+/FeatureServer)"', all_services) | ||
|
||
featureserver_urls = [f'{self.prefix}{redirect}' for redirect in featureserver_redirects] | ||
return ( | ||
pd.concat(self._get_layers_in_featureservers(url) for url in featureserver_urls) | ||
.drop_duplicates('description', keep='last') # Keep only the most recent data | ||
.reset_index(drop=True) | ||
) | ||
|
||
def search_for(self, search_for): | ||
return self.df.loc[lambda d: d.description.str.lower().str.contains(search_for.lower())] |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
from openspoor import FeatureServerOverview | ||
import pytest | ||
|
||
|
||
@pytest.fixture(scope='session') | ||
def all_featureserver_layers(): | ||
return FeatureServerOverview() | ||
|
||
|
||
# TODO: These tests can be a lot stricter | ||
def test_get_all_featureserver_layers(all_featureserver_layers): | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Deze test faalt als mapservices een nieuwe feature toevoegd, terwijl dat deze functionaliteit niet zou breken. Misschien kan je deze test aanpassen dat die checkt dat bepaalde features die vaak gebruikt worden voorkomen in de lijst . There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. De tests zijn nu wat minder streng geworden; zo lang er nu enige layers gevonden worden (en het aantal layers met het woord spoor erin er meer zijn dan met het woord hek erin) dan komen de tests er doorheen. Voor de rest durfde ik het niet aan dat het aantal layers toe zal blijven nemen in de toekomst. |
||
out = all_featureserver_layers.df | ||
assert out.shape == (264, 2) | ||
|
||
|
||
# TODO: These tests can be a lot stricter | ||
def test_get_layers_in_featureservers(all_featureserver_layers): | ||
url = 'https://mapservices.prorail.nl/arcgis/rest/services/Bodemkwaliteit_001/FeatureServer' | ||
out = all_featureserver_layers._get_layers_in_featureservers(url) | ||
assert out.shape == (3, 2) | ||
|
||
|
||
# TODO: These tests can be a lot stricter | ||
def test_search_for(all_featureserver_layers): | ||
out = all_featureserver_layers.search_for('hek') | ||
assert out.shape == (3, 2) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Docstrings bij class en functies
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Toegevoegd!