I would like to use Feeds to import wkt data from CSV files. I have used the existing Feeds plugin to import point location by splitting the data into lat and long but I want import LINESTRING and MULTILINESTRING. For testing I am replacing the existing Feeds plugin but my aim is to make a plugin that works for lat/long and wkt.
For testing I have simplified the existing code to just pass the wkt string but this doesn't work so I obviously haven't got the format correct. Can someone help with the right format to pass wkt? The code I am using is below

<?php

namespace Drupal\geofield\Feeds\Target;

use Drupal\Core\Field\FieldDefinitionInterface;
use Drupal\feeds\Exception\EmptyFeedException;
use Drupal\feeds\FieldTargetDefinition;
use Drupal\feeds\Plugin\Type\Target\FieldTargetBase;

/**
 * Defines a geofield field mapper.
 *
 * @FeedsTarget(
 *   id = "geofield_feeds_target",
 *   field_types = {"geofield"}
 * )
 */
class Geofield extends FieldTargetBase {

  /**
   * {@inheritdoc}
   */
  protected static function prepareTarget(FieldDefinitionInterface $field_definition) {
    return FieldTargetDefinition::createFromFieldDefinition($field_definition)
      ->addProperty('wkt');
  }

  /**
   * {@inheritdoc}
   */
  protected function prepareValue($delta, array &$values) {

    if (isset($values['wkt']) && empty($values['wkt'])) {
       // If wkt is empty set it to null
       $values['wkt'] = NULL;
     }
     dpm($values['wkt']);
  }
}

All help appreciated

Support from Acquia helps fund testing for Drupal Acquia logo

Comments

martinst created an issue. See original summary.

mmaranao’s picture

@martinst

We were having the same issue as well wherein the source was list of lat/lon pairs from a JSON and it was not validating because there were multiple points. So we've added a feed target setting for a geofield format to return the list of lat/lon pairs as a string value of pairs as a Polygon.

I've attached the patch against the latest geofield module (8.x-1.6) and this could possibly work for you with some minor tweaks.

Hope that helps.

martinst’s picture

@mmaranao

Thank you for the patch which has helped provide some answers. It doesn't work for me directly but I will keep digging. What I need to add is a change of input fields from lat/long to a single item when WKT is selected. This input will take LINESTRING(X Y, X Y ....). I also need to bypass the POLYGON creation as I may be passing a LINESTRING, MULTILINESTRING or POLYGON. If I solve my problem I will post a patch.

Thanks for the help

itamair’s picture

any progress on this side?
Let us know when a new well tested and reviewed working patch is provided, that I will commit ...

martinst’s picture

I now have a patch that allows both wkt and lat/long data to be imported. The patch removes code which was not necessary to import lat/long points. I have added a 3rd target field which is for the wkt data. To make this work the addProperty has to be 'value' as this matched the last part of the database field name. The patch allows multiple lat/long or wkt fields on the same csv line. What doesn't work is loading multiple values into the same field. This seems to be a problem with feeds as the prepareValue routine is called the same number of times as duplicate values i.e. if you want to load 3 values into a field prepareValue is called 9 times (3*3) rather than 3 times. You get 3 copies of the data in the field.

itamair’s picture

Status: Active » Needs review
demonde’s picture

Looks to me as if the patch does not work with the latest geofield version.

ken-g’s picture

Status: Needs review » Reviewed & tested by the community
FileSize
2.93 KB

Hi @itamair,

A few days ago I had been hoping to import some boundary data in WKT format into a Geofield. However, I hadn't realised that Feeds and Geofield currently can only process single point markers, even though Geofield itself can support multiple geometry types.

I found this outstanding issue which covers my requirements, but it looks as though the patch that was created a year ago no longer works against the latest Geofield version.

So, I have recreated the patch same against the latest version, and have spent some time testing it with various WKT geometries - POINT, POLYGON, GEOMETRYCOLLECTION, etc. All work well.

Finally, in #5, @martinst questions how multi-value Geofields should be loaded via Feeds. The general consensus in the Feeds community is that multi-value fields (of any type) should first be "exploded" using a standard Feed Tamper plugin. I tested this patch in conjunction with Feeds Tamper, and was able to load 3 separate WKT geometries from a single CSV row into a multi-value Geofield in Drupal. I then displayed each separate geometry of the multi-value Geofield on its own Leaflet map. Again, looks OK to me.

In summary, my view the patch is good to go. Assuming you are also happy with the patch, is there any chance that you can commit this change before Christmas ?

itamair’s picture

thanks @ken-g ... many chances, gong to test and review this asap.

itamair’s picture

Title: Extend Feeds plugin to allow wkt » Extend Feeds plugin to allow import of WKY and GeoJson
itamair’s picture

Status: Reviewed & tested by the community » Fixed
FileSize
3.58 KB

I tested and reviewed the #8 patch, and actually I found some inaccuracies and issues:

- not accurately compliant with Drupal/Php coding standards (please note that is a necessary condition to pass the review and be committed/merged, so please go through them for being compliant next time);
- it wouldn't allow the simultaneous import of Lat/Lon couples and Geometries for the same record (the Geometry would have prevailed over and canceled the Points import);

The new attached patch fixes all the above, and has been successfully tested by me locally.

Please NOTE that this new functionality allows the straight import of both (valid) WKT and Geojson strings/data. Hence I also adjusted this issue title accordingly ...

I am going to commit it right now in the Geofield 8.x-1.x-dev branch, and is going to be part of the next module release.

Please reopen this (or a new related one) if you experience any new issues on this.

  • itamair committed ac06160 on 8.x-1.x
    Issue #3094659 by itamair, martinst, ken-g, mmaranao: Extend Feeds...
ken-g’s picture

Title: Extend Feeds plugin to allow import of WKY and GeoJson » Extend Feeds plugin to allow import of WKT and GeoJson

Correcting typo in title.

Status: Fixed » Closed (fixed)

Automatically closed - issue fixed for 2 weeks with no activity.

BobBulman’s picture

I'm trying to do the same thing - import multilinestring geodata into Drupal to be displayed on Leaflet maps, I got this working on our test server (PHP 7.4.19) but our production server is PHP 8.1.5, we have applied the patch above but still get an error when I run the feed. Does anyone have a link to a youtube video or working example I can follow please?

ekes’s picture

I'm trying to do the same thing - import multilinestring geodata into Drupal to be displayed on Leaflet maps, I got this working on our test server (PHP 7.4.19) but our production server is PHP 8.1.5, we have applied the patch above but still get an error when I run the feed. Does anyone have a link to a youtube video or working example I can follow please?

I think https://www.drupal.org/project/geofield/issues/2952745#comment-14359248 will fix your issue.

itamair’s picture

Status: Closed (fixed) » Closed (duplicate)
Parent issue: » #3273822: Feeds integration: Geometry mapping