Import Geo Data from German/Hessen AgrarSystem

I modified the KmlImport so if the kml file contains ExtendedData, the unempty values like DEHELI0006305492 are imported and can be used further. If a part named lage_bez exists it is used as Asset Name.

Sample

<Data name=“lage_bez“> <value>Schießloch</value> </Data>
<Data name=“flik_aktue“> <value>DEHELI0006305492</value> </Data>

Should not affect behaviour of kml files without this data and can replace the existing importer. Would be nice to can add dialects or subparsers in Farmos 4.

farmos_SAF_Plugins/Germany/Hessen/importer at main · Me-xx/farmos_SAF_Plugins

Just for my curiosity, I’m curious how the changes you made achieve what you’re describing. Naively, the diff doesn’t seem like it would have much of an effect…

diff --git a/KmlImporter.original.php b/KmlImporter.modified.php
index 7001d67..9a34f28 100644
--- a/KmlImporter.original.php
+++ b/KmlImporter.modified.php
@@ -1,5 +1,15 @@
 <?php
-
+/**
+Handels parts like
+                               <Data name="lage_bez">
+                                       <value>Schießloch</value>
+                               </Data>
+as Name And
+                               <Data name="flik_aktue">
+                                       <value>DEHELI0006305492</value>
+                               </Data>
+as Notes
+**/
 declare(strict_types=1);
 
 namespace Drupal\farm_import_kml\Form;
@@ -95,9 +105,7 @@ class KmlImporter extends FormBase {
       '#description' => $this->t('Upload your KML file here and click "Parse".'),
       '#upload_location' => 'private://kml',
       '#upload_validators' => [
-        'FileExtension' => [
-          'extensions' => 'kml kmz',
-        ],
+        'file_validate_extensions' => ['kml kmz'],
       ],
       '#required' => TRUE,
     ];

Thanks for the hint … first version was wrong … now it should work

That’s more like it :grin:

Reading through the new diff, it looks like you’re getting some key/value pairs from an ExtendedData object on each placemark. Then some of these get used to populate the name and notes for the resulting asset.

It looks like you’ve got some code to map one of those keys - “ncode_aktu” from a numeric value into a string “Paddock” or “Landmark”, but I don’t see the subsequent code to use that as the land type. Is that intentional?

diff --git a/Germany/Hessen/importer/KmlImporter.php b/Germany/Hessen/importer/KmlImporter.php
index 9a34f28..544ca06 100644
--- a/Germany/Hessen/importer/KmlImporter.php
+++ b/Germany/Hessen/importer/KmlImporter.php
@@ -1,5 +1,5 @@
 <?php
-/**
+/*
 Handels parts like
 				<Data name="lage_bez">
 					<value>Schießloch</value>
@@ -9,7 +9,7 @@
 					<value>DEHELI0006305492</value>
 				</Data>
 as Notes
-**/
+*/
 declare(strict_types=1);
 
 namespace Drupal\farm_import_kml\Form;
@@ -70,6 +70,18 @@ public function __construct(EntityTypeManagerInterface $entity_type_manager, Ser
     $this->fileSystem = $file_system;
   }
 
+	protected function extractExtendedData(\SimpleXMLElement $placemark): array {
+	  $data = [];
+	  foreach ($placemark->ExtendedData->Data as $entry) {
+		$name = (string) $entry['name'];
+		$value = trim((string) $entry->value);
+		if ($value !== '') {
+		  $data[$name] = $value;
+		}
+	  }
+	  return $data;
+	}
+
   /**
    * {@inheritdoc}
    */
@@ -181,6 +193,33 @@ public function buildForm(array $form, FormStateInterface $form_state) {
     ];
     foreach ($geometries as $index => $geometry) {
 
+        // Versuche das zugehörige Placemark aus dem KML zu holen
+        $placemarks = new \SimpleXMLElement($data);
+        $placemark = $placemarks->Document->Placemark[$index] ?? null;
+
+        $extended = $placemark ? $this->extractExtendedData($placemark) : [];
+
+        // Mapping für ncode_aktu
+        if (isset($extended['ncode_aktu'])) {
+          $map = ['459' => 'Paddock', '480' => 'Landmark'];
+          $extended['ncode_aktu_mapped'] = $map[$extended['ncode_aktu']] ?? $extended['ncode_aktu'];
+        }
+
+        // Hinweise als XML zusammenbauen
+        $notes = '';
+        foreach ($extended as $key => $value) {
+          if (!str_contains($key, '_mapped') && $key !== 'lage_bez') {
+            $notes .= "<Data name=\"$key\"><value>$value</value></Data>\n";
+          }
+        }
+
+        // Überschreibe Name, falls lage_bez vorhanden
+        $name = $extended['lage_bez'] ?? ($geometry->properties['name'] ?? '');
+
+        // Notes überschreiben
+        $description = $notes ?: ($geometry->properties['description'] ?? '');
+
+
       // Create a fieldset for the geometry.
       $form['output']['assets'][$index] = [
         '#type' => 'fieldset',
@@ -190,7 +229,7 @@ public function buildForm(array $form, FormStateInterface $form_state) {
       $form['output']['assets'][$index]['name'] = [
         '#type' => 'textfield',
         '#title' => $this->t('Name'),
-        '#default_value' => $geometry->properties['name'] ?? '',
+        '#default_value' => $name ?? '',
         '#required' => TRUE,
       ];
 
@@ -205,7 +244,7 @@ public function buildForm(array $form, FormStateInterface $form_state) {
       $form['output']['assets'][$index]['notes'] = [
         '#type' => 'textarea',
         '#title' => $this->t('Notes'),
-        '#default_value' => $geometry->properties['description'] ?? '',
+        '#default_value' => $description ?? '',
       ];
 
       $form['output']['assets'][$index]['geometry'] = [

Thanks for the detailed Review … missed that fact during my import :frowning:
$form['output']['assets'][$index]['land_type'] = [ '#type' => 'select', '#title' => $this->t('Land type'), '#options' => $land_type_options, '#default_value' => $form_state->getValue('land_type'), '#required' => TRUE, ];

Must be changed to use either the mapped value OR the default from here.
// Build land type options. $land_type_options = farm_land_type_options(); $form['input']['land_type'] = [ '#type' => 'select', '#title' => $this->t('Default land type'), '#description' => $this->t('Specify the default land type for the assets in this KML. This can be overridden below on a per-asset basis before creating the assets.'), '#options' => $land_type_options, '#required' => TRUE, ];

So this should be right
// Mapping für ncode_aktu if (isset($extended['ncode_aktu'])) { $map = ['459' => 'Paddock', '480' => 'Landmark']; $ncode_landType= $map[$extended['ncode_aktu']] ?? $form_state->getValue('land_type'); }
and
$form['output']['assets'][$index]['land_type'] = [ '#type' => 'select', '#title' => $this->t('Land type'), '#options' => $land_type_options, '#default_value' => $ncode_landType, '#required' => TRUE, ];

but havend checked it yet