{ "cells": [ { "cell_type": "markdown", "metadata": {}, "source": [ "# Examples of object type customization" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "[![Binder](https://mybinder.org/badge_logo.svg)](https://mybinder.org/v2/gh/DOV-Vlaanderen/pydov/master?filepath=docs%2Fnotebooks%2Fcustomizing_object_types.ipynb)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "We believe all pydov types to have the most relevant fields available by default. However, more data might be available and of interest to your use case.\n", "\n", "The pydov library provides a flexible way to customize the output of your data searches. One of the key features is the ability to customize the object types used in the search process. This notebook will demonstrate the different ways you can customize object types in pydov." ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "# Defining Custom Object Types in pydov\n", "\n", "The pydov library provides a flexible way to customize the output of your data searches. One of the key features is the ability to define your own custom object types, which allows you to include specific XML fields that are not part of the default object types.\n", "\n", "This notebook will demonstrate three different ways you can define custom object types in pydov:\n", "\n", "1. Adding an XML field to an existing main type\n", "2. Adding an XML field to an existing subtype\n", "3. Creating a new subtype" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Adding an XML field to a main type\n", "\n", "To add an extra XML field to an existing main type, you can use the `with_extra_fields` method. This allows you to create a custom object type that includes additional fields beyond the default set." ] }, { "cell_type": "code", "execution_count": 1, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "[000/001] .\n", "[000/010] cccccccccc\n" ] }, { "data": { "text/html": [ "
\n", "\n", "\n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", "
pkey_boringboornummerxymv_mtawstart_boring_mtawgemeentediepte_boring_vandiepte_boring_totdatum_aanvanguitvoerderboorgatmetingmethode_xydiepte_methode_vandiepte_methode_totboormethode
0https://www.dov.vlaanderen.be/data/boring/2016...kb29d84e-B57492424.0170752.040.0040.00Wortegem-Petegem0.00.0NaNonbekendFalsegedigitaliseerd op topokaart0.00.0onbekend
1https://www.dov.vlaanderen.be/data/boring/2016...kb29d84e-B57592296.0170885.046.0046.00Wortegem-Petegem0.00.0NaNonbekendFalsegedigitaliseerd op topokaart0.00.0onbekend
2https://www.dov.vlaanderen.be/data/boring/1945...kb22d55e-B1037108890.0188690.014.3114.31Melle0.015.01945-01-01De Meyer L.-GentFalsegedigitaliseerd op topokaart0.015.0droge boring
3https://www.dov.vlaanderen.be/data/boring/2016...kb29d84e-B57692243.0170939.047.5047.50Wortegem-Petegem0.00.0NaNonbekendFalsegedigitaliseerd op topokaart0.00.0onbekend
4https://www.dov.vlaanderen.be/data/boring/2016...kb29d84e-B57792475.0171088.039.0039.00Wortegem-Petegem0.00.0NaNonbekendFalsegedigitaliseerd op topokaart0.00.0onbekend
\n", "
" ], "text/plain": [ " pkey_boring boornummer \\\n", "0 https://www.dov.vlaanderen.be/data/boring/2016... kb29d84e-B574 \n", "1 https://www.dov.vlaanderen.be/data/boring/2016... kb29d84e-B575 \n", "2 https://www.dov.vlaanderen.be/data/boring/1945... kb22d55e-B1037 \n", "3 https://www.dov.vlaanderen.be/data/boring/2016... kb29d84e-B576 \n", "4 https://www.dov.vlaanderen.be/data/boring/2016... kb29d84e-B577 \n", "\n", " x y mv_mtaw start_boring_mtaw gemeente \\\n", "0 92424.0 170752.0 40.00 40.00 Wortegem-Petegem \n", "1 92296.0 170885.0 46.00 46.00 Wortegem-Petegem \n", "2 108890.0 188690.0 14.31 14.31 Melle \n", "3 92243.0 170939.0 47.50 47.50 Wortegem-Petegem \n", "4 92475.0 171088.0 39.00 39.00 Wortegem-Petegem \n", "\n", " diepte_boring_van diepte_boring_tot datum_aanvang uitvoerder \\\n", "0 0.0 0.0 NaN onbekend \n", "1 0.0 0.0 NaN onbekend \n", "2 0.0 15.0 1945-01-01 De Meyer L.-Gent \n", "3 0.0 0.0 NaN onbekend \n", "4 0.0 0.0 NaN onbekend \n", "\n", " boorgatmeting methode_xy diepte_methode_van \\\n", "0 False gedigitaliseerd op topokaart 0.0 \n", "1 False gedigitaliseerd op topokaart 0.0 \n", "2 False gedigitaliseerd op topokaart 0.0 \n", "3 False gedigitaliseerd op topokaart 0.0 \n", "4 False gedigitaliseerd op topokaart 0.0 \n", "\n", " diepte_methode_tot boormethode \n", "0 0.0 onbekend \n", "1 0.0 onbekend \n", "2 15.0 droge boring \n", "3 0.0 onbekend \n", "4 0.0 onbekend " ] }, "execution_count": 1, "metadata": {}, "output_type": "execute_result" } ], "source": [ "from pydov.search.boring import BoringSearch\n", "from pydov.types.boring import Boring\n", "from pydov.types.fields import XmlField\n", "\n", "# Create a custom Boring type with an extra 'methode_xy' field\n", "MyBoring = Boring.with_extra_fields([\n", " XmlField(name='methode_xy',\n", " source_xpath='/boring/ligging/metadata_locatiebepaling/methode',\n", " datatype='string')\n", "])\n", "\n", "# Search using the custom Boring type\n", "bs = BoringSearch(objecttype=MyBoring)\n", "df = bs.search(max_features=10)\n", "df.head()" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "In this example, we've created a custom `MyBoring` type that includes an extra `methode_xy` field, which is retrieved from the XML document. We can then use this custom type in our search to include the additional field in the output dataframe." ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Adding an XML field to a subtype\n", "\n", "Similar to adding fields to a main type, you can also add extra XML fields to an existing subtype. This can be useful if you need to include additional data that is related to an existing subtype." ] }, { "cell_type": "code", "execution_count": 2, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "[000/001] .\n", "[000/003] ccc\n" ] }, { "data": { "text/html": [ "
\n", "\n", "\n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", "
pkey_filterpkey_grondwaterlocatiegw_idfilternummerfiltertypexystart_grondwaterlocatie_mtawmv_mtawgemeente...diepte_onderkant_filterlengte_filterdatumtijdstippeil_mtawbetrouwbaarheidmethodefilterstatusfiltertoestandopmeter
0https://www.dov.vlaanderen.be/data/filter/2003...https://www.dov.vlaanderen.be/data/put/2017-00...611/72/112peilfilter223063.2172028.3156.8256.82Kortessem...6.550.52004-04-06NaN55.02goedpeillintin rust1Labo
1https://www.dov.vlaanderen.be/data/filter/2003...https://www.dov.vlaanderen.be/data/put/2017-00...611/72/112peilfilter223063.2172028.3156.8256.82Kortessem...6.550.52004-05-19NaN54.45goedpeillintin rust1VMM - Afdeling Operationeel Waterbeheer
2https://www.dov.vlaanderen.be/data/filter/2003...https://www.dov.vlaanderen.be/data/put/2017-00...611/72/112peilfilter223063.2172028.3156.8256.82Kortessem...6.550.52004-12-14NaN55.11goedpeillintin rust1Labo
3https://www.dov.vlaanderen.be/data/filter/2003...https://www.dov.vlaanderen.be/data/put/2017-00...611/72/112peilfilter223063.2172028.3156.8256.82Kortessem...6.550.52005-02-24NaN55.97goedpeillintin rust1Labo
4https://www.dov.vlaanderen.be/data/filter/2003...https://www.dov.vlaanderen.be/data/put/2017-00...611/72/112peilfilter223063.2172028.3156.8256.82Kortessem...6.550.52005-09-29NaN53.46goedpeillintin rust1Labo
\n", "

5 rows × 24 columns

\n", "
" ], "text/plain": [ " pkey_filter \\\n", "0 https://www.dov.vlaanderen.be/data/filter/2003... \n", "1 https://www.dov.vlaanderen.be/data/filter/2003... \n", "2 https://www.dov.vlaanderen.be/data/filter/2003... \n", "3 https://www.dov.vlaanderen.be/data/filter/2003... \n", "4 https://www.dov.vlaanderen.be/data/filter/2003... \n", "\n", " pkey_grondwaterlocatie gw_id filternummer \\\n", "0 https://www.dov.vlaanderen.be/data/put/2017-00... 611/72/11 2 \n", "1 https://www.dov.vlaanderen.be/data/put/2017-00... 611/72/11 2 \n", "2 https://www.dov.vlaanderen.be/data/put/2017-00... 611/72/11 2 \n", "3 https://www.dov.vlaanderen.be/data/put/2017-00... 611/72/11 2 \n", "4 https://www.dov.vlaanderen.be/data/put/2017-00... 611/72/11 2 \n", "\n", " filtertype x y start_grondwaterlocatie_mtaw mv_mtaw \\\n", "0 peilfilter 223063.2 172028.31 56.82 56.82 \n", "1 peilfilter 223063.2 172028.31 56.82 56.82 \n", "2 peilfilter 223063.2 172028.31 56.82 56.82 \n", "3 peilfilter 223063.2 172028.31 56.82 56.82 \n", "4 peilfilter 223063.2 172028.31 56.82 56.82 \n", "\n", " gemeente ... diepte_onderkant_filter lengte_filter datum tijdstip \\\n", "0 Kortessem ... 6.55 0.5 2004-04-06 NaN \n", "1 Kortessem ... 6.55 0.5 2004-05-19 NaN \n", "2 Kortessem ... 6.55 0.5 2004-12-14 NaN \n", "3 Kortessem ... 6.55 0.5 2005-02-24 NaN \n", "4 Kortessem ... 6.55 0.5 2005-09-29 NaN \n", "\n", " peil_mtaw betrouwbaarheid methode filterstatus filtertoestand \\\n", "0 55.02 goed peillint in rust 1 \n", "1 54.45 goed peillint in rust 1 \n", "2 55.11 goed peillint in rust 1 \n", "3 55.97 goed peillint in rust 1 \n", "4 53.46 goed peillint in rust 1 \n", "\n", " opmeter \n", "0 Labo \n", "1 VMM - Afdeling Operationeel Waterbeheer \n", "2 Labo \n", "3 Labo \n", "4 Labo \n", "\n", "[5 rows x 24 columns]" ] }, "execution_count": 2, "metadata": {}, "output_type": "execute_result" } ], "source": [ "from pydov.search.grondwaterfilter import GrondwaterFilterSearch\n", "from pydov.types.grondwaterfilter import GrondwaterFilter, Peilmeting\n", "from pydov.types.fields import XmlField\n", "from pydov.util.location import WithinDistance, Point\n", "\n", "# Create a custom Peilmeting subtype with an extra 'opmeter' field\n", "MyPeilmeting = Peilmeting.with_extra_fields([\n", " XmlField(name='opmeter',\n", " source_xpath='/opmeter/naam',\n", " datatype='string')\n", "])\n", "\n", "# Create a custom GrondwaterFilter type with the custom Peilmeting subtype\n", "MyGrondwaterFilter = GrondwaterFilter.with_subtype(MyPeilmeting)\n", "\n", "# Search using the custom GrondwaterFilter type\n", "fs = GrondwaterFilterSearch(objecttype=MyGrondwaterFilter)\n", "df = fs.search(WithinDistance(Point(223063.20, 172028.31, epsg=31370), 10))\n", "df.head()" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "In this example, we've created a custom `MyPeilmeting` subtype that includes an extra `opmeter` field. We then used this custom subtype to create a new `MyGrondwaterFilter` main type, which we can use in our search." ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Creating a new subtype\n", "\n", "In addition to adding fields to existing object types, you can also define your own custom subtypes from scratch. This allows you to include specific XML fields that are not part of the default object subtypes." ] }, { "cell_type": "code", "execution_count": 3, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "[000/001] .\n", "[000/010] cccccccccc\n" ] }, { "data": { "text/html": [ "
\n", "\n", "\n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", "
pkey_sonderingsondeernummerxymv_mtawstart_sondering_mtawdiepte_sondering_vandiepte_sondering_totdatum_aanvanguitvoerdersondeermethodeapparaatdatum_gw_metingdiepte_gw_mtechniek_dieptetechniektechniek_andere
0https://www.dov.vlaanderen.be/data/sondering/2...VLA08-3.2-S16218343.8211622.951.1551.150.012.802009-02-17Labo Devlieger - Van Voorendiscontinu mechanisch200 kNNaTNaNNaNNaNNaN
1https://www.dov.vlaanderen.be/data/sondering/1...GEO-92/114-S4105658.0188808.0NaN12.450.02.701992-10-15MVG - Afdeling Geotechniekdiscontinu mechanisch25 kNNaTNaNNaNNaNNaN
2https://www.dov.vlaanderen.be/data/sondering/2...GEO-01/169-S23189313.7203475.9NaN21.401.215.152002-03-06MVG - Afdeling Geotechniekcontinu elektrisch200 kN - MAN22002-03-06 14:45:001.251.2VNaN
3https://www.dov.vlaanderen.be/data/sondering/2...GEO-01/169-S16189633.5203331.5NaN21.221.430.002002-03-11MVG - Afdeling Geotechniekdiscontinu mechanisch200 kN - MAN22002-03-11 13:50:001.229.2VNaN
4https://www.dov.vlaanderen.be/data/sondering/2...GEO-01/169-S16189633.5203331.5NaN21.221.430.002002-03-11MVG - Afdeling Geotechniekdiscontinu mechanisch200 kN - MAN22002-03-11 13:50:001.229.2VNaN
\n", "
" ], "text/plain": [ " pkey_sondering sondeernummer \\\n", "0 https://www.dov.vlaanderen.be/data/sondering/2... VLA08-3.2-S16 \n", "1 https://www.dov.vlaanderen.be/data/sondering/1... GEO-92/114-S4 \n", "2 https://www.dov.vlaanderen.be/data/sondering/2... GEO-01/169-S23 \n", "3 https://www.dov.vlaanderen.be/data/sondering/2... GEO-01/169-S16 \n", "4 https://www.dov.vlaanderen.be/data/sondering/2... GEO-01/169-S16 \n", "\n", " x y mv_mtaw start_sondering_mtaw diepte_sondering_van \\\n", "0 218343.8 211622.9 51.15 51.15 0.0 \n", "1 105658.0 188808.0 NaN 12.45 0.0 \n", "2 189313.7 203475.9 NaN 21.40 1.2 \n", "3 189633.5 203331.5 NaN 21.22 1.4 \n", "4 189633.5 203331.5 NaN 21.22 1.4 \n", "\n", " diepte_sondering_tot datum_aanvang uitvoerder \\\n", "0 12.80 2009-02-17 Labo Devlieger - Van Vooren \n", "1 2.70 1992-10-15 MVG - Afdeling Geotechniek \n", "2 15.15 2002-03-06 MVG - Afdeling Geotechniek \n", "3 30.00 2002-03-11 MVG - Afdeling Geotechniek \n", "4 30.00 2002-03-11 MVG - Afdeling Geotechniek \n", "\n", " sondeermethode apparaat datum_gw_meting diepte_gw_m \\\n", "0 discontinu mechanisch 200 kN NaT NaN \n", "1 discontinu mechanisch 25 kN NaT NaN \n", "2 continu elektrisch 200 kN - MAN2 2002-03-06 14:45:00 1.25 \n", "3 discontinu mechanisch 200 kN - MAN2 2002-03-11 13:50:00 1.22 \n", "4 discontinu mechanisch 200 kN - MAN2 2002-03-11 13:50:00 1.22 \n", "\n", " techniek_diepte techniek techniek_andere \n", "0 NaN NaN NaN \n", "1 NaN NaN NaN \n", "2 1.2 V NaN \n", "3 9.2 V NaN \n", "4 9.2 V NaN " ] }, "execution_count": 3, "metadata": {}, "output_type": "execute_result" } ], "source": [ "from pydov.search.sondering import SonderingSearch\n", "from pydov.types.sondering import Sondering\n", "from pydov.types.abstract import AbstractDovSubType\n", "from pydov.types.fields import XmlField\n", "\n", "# Define a custom Technieken subtype\n", "class Technieken(AbstractDovSubType):\n", " rootpath = './/sondering/sondeonderzoek/penetratietest/technieken'\n", " fields = [\n", " XmlField(name='techniek_diepte',\n", " source_xpath='/diepte_techniek',\n", " datatype='float'),\n", " XmlField(name='techniek',\n", " source_xpath='/techniek',\n", " datatype='string'),\n", " XmlField(name='techniek_andere',\n", " source_xpath='/techniek_andere',\n", " datatype='string')\n", " ]\n", "\n", "# Create a custom Sondering type with the Technieken subtype\n", "MySondering = Sondering.with_subtype(Technieken)\n", "\n", "# Search using the custom Sondering type\n", "ms = SonderingSearch(objecttype=MySondering)\n", "df = ms.search(max_features=10)\n", "df.head()" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "In this example, we've defined a custom `Technieken` subtype that includes three new XML fields: `techniek_diepte`, `techniek`, and `techniek_andere`. These fields will be retrieved from the XML document and included in the output dataframe.\n", "\n", "We then used the `with_subtype` method to create a custom `MySondering` object type that includes the `Technieken` subtype. This allows us to search for sonderingen (cone penetration tests) and retrieve the additional subtype fields in the output.\n", "\n", "By defining custom subtypes, you can tailor the output dataframe to include the specific data that is most relevant for your use case. This provides a high degree of flexibility and control over the data you retrieve from the pydov data sources." ] } ], "metadata": { "kernelspec": { "display_name": ".venv (3.13.5)", "language": "python", "name": "python3" }, "language_info": { "codemirror_mode": { "name": "ipython", "version": 3 }, "file_extension": ".py", "mimetype": "text/x-python", "name": "python", "nbconvert_exporter": "python", "pygments_lexer": "ipython3", "version": "3.13.5" } }, "nbformat": 4, "nbformat_minor": 4 }