@@ -182,6 +182,69 @@ def orient_order_by_structure(
182182 return agree , disagree
183183
184184
185+ def split_override_contacts (contacts , override_units , stratigraphic_order ):
186+ """Take the contacts of the override units out of the contacts.
187+
188+ The basal contact of an override unit (for example cover or an intrusion)
189+ is its full boundary with all other units. It does not come from the
190+ stratigraphic order. Thus each contact that touches an override unit is
191+ a basal contact of that unit, and it is not given to the other unit.
192+
193+ Parameters
194+ ----------
195+ contacts : GeoDataFrame
196+ All contacts, with 'UNITNAME_1' and 'UNITNAME_2' columns.
197+ override_units : list
198+ Names of the override units.
199+ stratigraphic_order : list
200+ Unit names, youngest first. If a contact is between two override
201+ units, it is the basal contact of the younger unit. A unit that is
202+ not in this list is younger than the units that are.
203+
204+ Returns
205+ -------
206+ tuple(GeoDataFrame, GeoDataFrame)
207+ (the contacts that do not touch an override unit, the basal contacts
208+ of the override units with 'ID', 'basal_unit', 'type' and 'geometry'
209+ columns, as map2loop gives them)
210+ """
211+ import shapely
212+
213+ override_units = [str (unit ).strip () for unit in override_units or [] if unit is not None ]
214+ override_units = [unit for unit in override_units if unit ]
215+ columns = ['ID' , 'basal_unit' , 'type' , 'geometry' ]
216+ if contacts is None :
217+ return contacts , pd .DataFrame (columns = columns )
218+ if not override_units or len (contacts ) == 0 :
219+ return contacts , contacts .iloc [0 :0 ].reindex (columns = columns )
220+
221+ unit_1 = contacts ['UNITNAME_1' ].astype (str ).str .strip ()
222+ unit_2 = contacts ['UNITNAME_2' ].astype (str ).str .strip ()
223+ touches = unit_1 .isin (override_units ) | unit_2 .isin (override_units )
224+ remaining = contacts [~ touches ].reset_index (drop = True )
225+ overridden = contacts [touches ].reset_index (drop = True )
226+
227+ order = [str (name ).strip () for name in stratigraphic_order if name is not None ]
228+
229+ def age_rank (unit ):
230+ return order .index (unit ) if unit in order else - 1
231+
232+ def owner (row ):
233+ a = str (row ['UNITNAME_1' ]).strip ()
234+ b = str (row ['UNITNAME_2' ]).strip ()
235+ if a in override_units and b in override_units :
236+ return a if age_rank (a ) <= age_rank (b ) else b
237+ return a if a in override_units else b
238+
239+ overridden ['basal_unit' ] = [owner (row ) for _ , row in overridden .iterrows ()]
240+ overridden ['ID' ] = [age_rank (unit ) for unit in overridden ['basal_unit' ]]
241+ overridden ['type' ] = 'BASAL'
242+ overridden ['geometry' ] = [
243+ shapely .line_merge (shapely .snap (geo , geo , 1 )) for geo in overridden ['geometry' ]
244+ ]
245+ return remaining , overridden [columns ]
246+
247+
185248def extract_basal_contacts (
186249 geology ,
187250 stratigraphic_order ,
@@ -193,6 +256,7 @@ def extract_basal_contacts(
193256 debug_manager = None ,
194257 target_crs = None ,
195258 unit_colours = None ,
259+ basal_override_units = None ,
196260):
197261 """Extract basal contacts from geological data.
198262
@@ -221,6 +285,11 @@ def extract_basal_contacts(
221285 Mapping of unit name to colour. When given, a 'colour' column is
222286 added to the returned basal contacts, looked up by the 'basal_unit'
223287 each contact was extracted for.
288+ basal_override_units : list, optional
289+ Names of units (for example cover or an intrusion) whose basal contact
290+ is their full boundary with all other units, not the contacts that the
291+ stratigraphic order gives. These units do not have to be in the
292+ stratigraphic column. See split_override_contacts.
224293
225294 Returns
226295 -------
@@ -259,7 +328,12 @@ def extract_basal_contacts(
259328 str (name ).strip () for name in stratigraphic_order if name is not None
260329 }
261330 ignored_names = {str (unit ).strip () for unit in ignore_units if unit is not None }
262- missing_from_column = sorted (geology_unit_names - stratigraphic_names - ignored_names )
331+ override_names = {
332+ str (unit ).strip () for unit in basal_override_units or [] if unit is not None
333+ }
334+ missing_from_column = sorted (
335+ geology_unit_names - stratigraphic_names - ignored_names - override_names
336+ )
263337 if missing_from_column :
264338 message = (
265339 "The geology layer has unit(s) with no entry in the stratigraphic column: "
@@ -278,6 +352,7 @@ def extract_basal_contacts(
278352 "ignore_units" : ignore_units ,
279353 "unit_name_field" : unit_name_field ,
280354 "all_contacts" : all_contacts ,
355+ "basal_override_units" : basal_override_units ,
281356 "geology" : geology ,
282357 "faults" : faults ,
283358 },
@@ -304,14 +379,22 @@ def extract_basal_contacts(
304379
305380 try :
306381 all_contacts_result = contact_extractor .extract_all_contacts ()
382+ column_contacts , override_contacts = split_override_contacts (
383+ all_contacts_result , basal_override_units , stratigraphic_order
384+ )
307385 # map2loop raises if a contact has a unit that is not in the column,
308386 # so give it only the contacts between units that are in the column.
309387 column_units = [name for name in stratigraphic_order if name is not None ]
310- contact_extractor .contacts = all_contacts_result [
311- all_contacts_result ['UNITNAME_1' ].isin (column_units )
312- & all_contacts_result ['UNITNAME_2' ].isin (column_units )
388+ contact_extractor .contacts = column_contacts [
389+ column_contacts ['UNITNAME_1' ].isin (column_units )
390+ & column_contacts ['UNITNAME_2' ].isin (column_units )
313391 ].reset_index (drop = True )
314- basal_contacts = contact_extractor .extract_basal_contacts (column_units )
392+ if len (contact_extractor .contacts ) > 0 :
393+ basal_contacts = contact_extractor .extract_basal_contacts (column_units )
394+ else :
395+ basal_contacts = override_contacts .iloc [0 :0 ]
396+ if len (override_contacts ) > 0 :
397+ basal_contacts = pd .concat ([basal_contacts , override_contacts ], ignore_index = True )
315398 logger .debug (
316399 "Extracted contacts: all=%s basal=%s" ,
317400 all_contacts_result .shape ,
0 commit comments