Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

forceCreate arg not usable when creating new Polygon from JSON coordinates field #5

Open
brandonb927 opened this issue Jan 10, 2023 · 5 comments

Comments

@brandonb927
Copy link

It seems as though, after attempting to use this fork to parse Twitter API GeoJSON data from this page (which parses fine in the original version of geoPHP), I am unable to use it for my use case. The coordinates in the example below are ripped directly from the Twitter API docs.

$json = "{
    \"coordinates\": [
        [
            [-74.026675, 40.683935],
            [-74.026675, 40.877483],
            [-73.910408, 40.877483],
            [-73.910408, 40.3935]
        ]
    ],
    \"type\": \"Polygon\"
}";

geoPHP::load(json_decode($json, true), 'json'); // <= This throws and exception with 'Cannot create Polygon: contains non-closed ring'

My understanding of the new Polygon class is that the constructor allows passing of a param 'forceCreate' => true to allow the Polygon to resolve itself to the start point, however there is no way to pass this arg through from geoPHP::load.

@mprins
Copy link

mprins commented Jan 10, 2023

load() has a variable arguments list as the last argument, does that not work for you?

geoPHP/src/geoPHP.php

Lines 129 to 142 in facd8fb

/**
* Load from an adapter format (like wkt) into a Geometry.
*
* If $data is array, all passed values will be combined into a single geometry.
*
* @param string|string[]|Geometry|Geometry[] $data The data in any supported format, including geoPHP Geometry.
* @param string $format Format of the data ('wkt','wkb','json', …). Tries to detect automatically if omitted.
* @param mixed $args Further arguments will be passed to the geo adapter.
*
* @throws \Exception|IOException
*
* @return Geometry
*/
public static function load($data, string $format = null, ...$args): Geometry

@brandonb927
Copy link
Author

load() has a variable arguments list as the last argument, does that not work for you?

geoPHP/src/geoPHP.php

Lines 129 to 142 in facd8fb

/**
* Load from an adapter format (like wkt) into a Geometry.
*
* If $data is array, all passed values will be combined into a single geometry.
*
* @param string|string[]|Geometry|Geometry[] $data The data in any supported format, including geoPHP Geometry.
* @param string $format Format of the data ('wkt','wkb','json', …). Tries to detect automatically if omitted.
* @param mixed $args Further arguments will be passed to the geo adapter.
*
* @throws \Exception|IOException
*
* @return Geometry
*/
public static function load($data, string $format = null, ...$args): Geometry

Yes, using the original example data, I have tried the following

geoPHP::load(json_decode($json, true), 'json', ['forceCreate' => true];

but it did not change anything.

@BathoryPeter
Copy link
Member

Both OGC specification and GeoJSON specification requires polygon rings to be closed, but I don't think Twitter has any developers left who can fix this :)

Extra arguments of geoPHP::load() are passed to adapters only. They can be used to control input/output format (e.g. endianness), not to modify the internal behaviour of the adapter.

I think we should add a static option to geoPHP class, e.g. tolerantMode and change adapters and geometries to tolerate some non critical anomalies in input data with this mode enabled.

@brandonb927
Copy link
Author

I think we should add a static option to geoPHP class, e.g. tolerantMode and change adapters and geometries to tolerate some non critical anomalies in input data with this mode enabled.

That sounds like a non-trivial amount of work that I doubt is high on anyone's priority list, so I will just keep tabs on this issue to see if anyone else runs into this like I did and if they came up with a solution.

@brandonb927
Copy link
Author

What could be a good stop-gap in the meantime though is having the ability to configure per-type options and pass them through the geoPHP::load() args like

geoPHP::load(
	json_decode($json, true),
	'json',
	[
		'typeArgs' => {
			'Polygon' => [
				'forceCreate' => true,
			],
		],
	],
);

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants