Skip to content

Instantly share code, notes, and snippets.

@sreejithpro
Last active April 19, 2023 13:48
Show Gist options
  • Save sreejithpro/bffa4b8a47044fec81b054671daace56 to your computer and use it in GitHub Desktop.
Save sreejithpro/bffa4b8a47044fec81b054671daace56 to your computer and use it in GitHub Desktop.
python script to download a snip of bedrock and superficial geology of a location from BGS maps. It also copies the geology to an excel sheet.
from selenium import webdriver
from selenium.webdriver.common.by import By
from selenium.webdriver.edge.service import Service
from selenium.webdriver.common.keys import Keys
from selenium.webdriver.common.action_chains import ActionChains
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
from selenium.common.exceptions import TimeoutException
import time
import pandas as pd
df = pd.read_excel(r'resources\Coordinates.xlsx')
if __name__ == '__main__':
for index in range(len(df)):
locationCords = df['Location Coords'].loc[index]
structureName = df['Structure Name'].loc[index]
service = Service('msedgedriver.exe')
# options = webdriver.EdgeOptions
# options.add_argument("--window-position=1920,0")
# options.add_argument("--window-size=1920,1080")
edgeBrowser = webdriver.Edge(service=service)
edgeBrowser.set_window_position(2000, 0)
edgeBrowser.get('https://geologyviewer.bgs.ac.uk/')
edgeBrowser.maximize_window()
edgeBrowser.implicitly_wait(5)
edgeBrowser.find_element(By.XPATH, "/html/body/app-root/app-components/app-nav-bar/mat-toolbar"
"/div[3]/button/span[4]").click()
edgeBrowser.implicitly_wait(5)
addressBox = edgeBrowser.find_element(By.XPATH, '/html/body/app-root/app-components/mat-drawer-container'
'/mat-drawer-content/app-search-bar/mat-toolbar/mat-toolbar-row'
'/div[2]/mat-form-field/div[1]/div[2]/div/input')
edgeBrowser.implicitly_wait(5)
addressBox.send_keys(str(locationCords))
time.sleep(1)
addressBox.send_keys(Keys.ENTER)
time.sleep(5)
body_element = edgeBrowser.find_element(By.TAG_NAME, "body")
action = ActionChains(edgeBrowser)
action.move_to_element_with_offset(body_element, 0, 40)
action.click()
action.perform()
time.sleep(3)
try:
element_present = WebDriverWait(edgeBrowser, 4).until(
EC.presence_of_element_located((By.XPATH, '/html/body/div[2]/div/div/mat-bottom-sheet-container/'
'app-marker-info/'
'app-base-bottom-sheet/div[2]/mat-accordion/mat-expansion-'
'panel[2]/'
'mat-expansion-panel-header')))
except TimeoutException:
superficialText = 'No Superficial Geology Present'
bedrock_element = edgeBrowser.find_element(By.XPATH, '/html/body/div[2]/div/div/mat-bottom-sheet-container'
'/app-marker-info/app-base-bottom-sheet/div[2]/div/div'
'/p')
else:
element_present.click()
edgeBrowser.implicitly_wait(10)
superficial_element = edgeBrowser.find_element(By.XPATH, '/html/body/div[2]/div/div/'
'mat-bottom-sheet-container/app-marker-info/'
'app-base-bottom-sheet/div[2]'
'/mat-accordion/mat-expansion-panel[2]/div/div/p')
superficialText = superficial_element.text
edgeBrowser.implicitly_wait(10)
bedrock_element = edgeBrowser.find_element(By.XPATH, '/html/body/div[2]/div/div/mat-bottom-sheet-container/'
'app-marker-info/app-base-bottom-sheet/div[2]/'
'mat-accordion'
'/mat-expansion-panel[1]/div/div/p')
bedrockText = bedrock_element.text
df.at[index, 'Bedrock Geology'] = bedrockText
df.at[index, 'Superficial Deposits'] = superficialText
df.to_excel(r'resources\Coordinates_ed.xlsx', index=False)
filename = r'screenshots/%s.png' % (str(structureName))
edgeBrowser.save_screenshot(filename)
time.sleep(2)
edgeBrowser.quit()
@sreejithpro
Copy link
Author

Revision 2: Updated the code for handling error when superficial geology is not present and other minor error handling.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment