davidpiesse / nova-map

Laravel Nova Map Field
114 stars 29 forks source link

Having trouble generating geojson data in Laravel Nova #16

Closed kfvit-andreaskviby closed 3 years ago

kfvit-andreaskviby commented 5 years ago

Hey Thanks for a great field type. I have created a variable called $geoData and when I output that with info($geoData) I get the below json.

{
    "type": "FeatureCollection",
    "features": [
        {
            "type": "Feature",
            "geometry": {
                "type": "LineString",
                "coordinates": [
                    [
                        17.648694,
                        59.857882000000004
                    ],
                    [
                        17.928489,
                        59.649072
                    ]
                ]
            },
            "properties": {
                "stroke": "#ff0025",
                "stroke-width": 3,
                "stroke-opacity": 1
            }
        }
    ]
} 

And when trying this data on the geojson.io service it will draw the line perfect for me on the map. But when I try to apply it to the Map Field Type in Laravel Nova I get a map focused in the middle of the sea far far away from Sweden and with no lines at all on the map.

Map::make('Plats')->spatialType('GeoJSON') ->geojson($geoData) ->height('400px'),

Please help if you can.

davidpiesse commented 5 years ago

Hi Andreas,

At first glace this is what the JS calls return JSON.parse(this.value)

So it should take all you have and render it. Have you tried with the bare minimum? "type": "Feature", "geometry": { "type": "LineString", "coordinates": [ [ 17.648694, 59.857882000000004 ], [ 17.928489, 59.649072 ] ]

It might be that the GeoJson parser for LeafletJS cant handle all the other parts of the geojson? There is also a way to force through your entire object without any json parsing.

Map::make('Plats')->spatialType('RandomString') ->geojson($geoData) ->height('400px'),

Let me know if any of this works for you.

David

On Thu, 21 Feb 2019 at 14:21, andreaskvibydialect notifications@github.com wrote:

Hey Thanks for a great field type. I have created a variable called $geoData and when I output that with info($geoData) I get the below json.

{ "type": "FeatureCollection", "features": [ { "type": "Feature", "geometry": { "type": "LineString", "coordinates": [ [ 17.648694, 59.857882000000004 ], [ 17.928489, 59.649072 ] ] }, "properties": { "stroke": "#ff0025", "stroke-width": 3, "stroke-opacity": 1 } } ] }

And when trying this data on the geojson.io service it will draw the line perfect for me on the map. But when I try to apply it to the Map Field Type in Laravel Nova I get a map focused in the middle of the sea far far away from Sweden and with no lines at all on the map.

Map::make('Plats')->spatialType('GeoJSON') ->geojson($geoData) ->height('400px'),

Please help if you can.

— You are receiving this because you are subscribed to this thread. Reply to this email directly, view it on GitHub https://github.com/davidpiesse/nova-map/issues/16, or mute the thread https://github.com/notifications/unsubscribe-auth/AAw3is2xPPWAh1tkBSzXd1NWXPVYQL_-ks5vPqtwgaJpZM4bHi6Y .

kfvit-andreaskviby commented 5 years ago

Hi Andreas, At first glace this is what the JS calls return JSON.parse(this.value) So it should take all you have and render it. Have you tried with the bare minimum? "type": "Feature", "geometry": { "type": "LineString", "coordinates": [ [ 17.648694, 59.857882000000004 ], [ 17.928489, 59.649072 ] ] It might be that the GeoJson parser for LeafletJS cant handle all the other parts of the geojson? There is also a way to force through your entire object without any json parsing. Map::make('Plats')->spatialType('RandomString') ->geojson($geoData) ->height('400px'), Let me know if any of this works for you. David On Thu, 21 Feb 2019 at 14:21, andreaskvibydialect @.***> wrote: Hey Thanks for a great field type. I have created a variable called $geoData and when I output that with info($geoData) I get the below json. { "type": "FeatureCollection", "features": [ { "type": "Feature", "geometry": { "type": "LineString", "coordinates": [ [ 17.648694, 59.857882000000004 ], [ 17.928489, 59.649072 ] ] }, "properties": { "stroke": "#ff0025", "stroke-width": 3, "stroke-opacity": 1 } } ] } And when trying this data on the geojson.io service it will draw the line perfect for me on the map. But when I try to apply it to the Map Field Type in Laravel Nova I get a map focused in the middle of the sea far far away from Sweden and with no lines at all on the map. Map::make('Plats')->spatialType('GeoJSON') ->geojson($geoData) ->height('400px'), Please help if you can. — You are receiving this because you are subscribed to this thread. Reply to this email directly, view it on GitHub <#16>, or mute the thread https://github.com/notifications/unsubscribe-auth/AAw3is2xPPWAh1tkBSzXd1NWXPVYQL_-ks5vPqtwgaJpZM4bHi6Y .

Hey It gives the same empty map with the below data

 {
    "type": "Feature",
    "geometry": {
        "type": "LineString",
        "coordinates": [
            [
                17.648694,
                59.857882000000004
            ],
            [
                17.928489,
                59.649072
            ]
        ]
    }
}  
kfvit-andreaskviby commented 5 years ago

Any news about this? Really need this for a client project. We must make geojson work because we will draw a lot of stuff using that kind of data in the backend.

davidpiesse commented 5 years ago

Hi Andreas,

I will try and get round to having a look tonight. In the mean time can you check what geojson is being passed to your front end? I will at least send some working geojson strings your way to confirm those work - and then hopefully that determines where the issue / challenge is coming from.

My thinking right now is the geojson is too complex for LeafletJS...

Trial and error till we know what part of the input geojson it doesn't like.

David

On Thu, 21 Feb 2019 at 16:43, andreaskvibydialect notifications@github.com wrote:

Any news about this? Really need this for a client project. We must make geojson work because we will draw a lot of stuff using that kind of data in the backend.

— You are receiving this because you commented. Reply to this email directly, view it on GitHub https://github.com/davidpiesse/nova-map/issues/16#issuecomment-466072410, or mute the thread https://github.com/notifications/unsubscribe-auth/AAw3irnr077pHCpNL02ikVO5J8OoD2Zjks5vPsycgaJpZM4bHi6Y .

davidpiesse commented 5 years ago

Hi @andreaskvibydialect

Did some testing and this geojson works fine for me....

Map::make('Location','geojson')
    ->spatialType('GeoJSON')
    ->height('300px'),

In this example geojson is the field name from the DB. I think I was too tired earlier - but the geojson function is meant to pass in the field name NOT the actual data...

Map::make('Plats')->spatialType('RandomString') ->geojson($geoData) ->height('400px'),

This is the excerpt from the code.

    public function geojson($geojson_field){
        $this->attribute = $geojson_field;

        return $this->withMeta([
            'geojson_field' => $geojson_field
        ]);
    }

If you can put the geojson string in the db row for the model you should be fine :) Hope that clears it up!

kfvit-andreaskviby commented 5 years ago

Ok so what you mean is that the package can't handle inserting a string as the GeoJson values for the component? The thing is that we take in lon,lat from another table using relationships between the two so we do not have the coordinates stored in the current table at all.

davidpiesse commented 5 years ago

As it stands yes. It could accept say ->rawGeoJSON() which takes a string.

You could also take your logic to make the geojson that you already have and pass it into a getXyzAttribute on the model (computed attribute)

D

On Fri, 22 Feb 2019, 08:29 andreaskvibydialect, notifications@github.com wrote:

Ok so what you mean is that the package can't handle inserting a string as the GeoJson values for the component? The thing is that we take in lon,lat from another table using relationships between the two so we do not have the coordinates stored in the current table at all.

— You are receiving this because you commented. Reply to this email directly, view it on GitHub https://github.com/davidpiesse/nova-map/issues/16#issuecomment-466315784, or mute the thread https://github.com/notifications/unsubscribe-auth/AAw3inzNOQNWyOeEQR4MvfqLwT4o6brtks5vP6pmgaJpZM4bHi6Y .

davidpiesse commented 5 years ago

@andreaskvibydialect Try the new master !!! Added RawGeojson 😃

Map::make('Location')
    ->rawGeojson($raw)
    ->spatialType('RawGeoJSON')
    ->height('300px'),
kfvit-andreaskviby commented 5 years ago

@andreaskvibydialect Try the new master !!! Added RawGeojson 😃

Map::make('Location')
    ->rawGeojson($raw)
    ->spatialType('RawGeoJSON')
    ->height('300px'),

Hey You are my hero, it works like a charm! Thank you so much for your efforts. Please let me know when you have support for the properties array like:

"type": "Feature",
    "geometry": {
        "type": "LineString",
        "coordinates": [
            [
                16.181679,
                58.596266
            ],
            [
                18.057267,
                59.329971
            ]
        ]
    },
    "properties": {
        "stroke": "#ff0ff",
        "stroke-width": 4,
        "stroke-opacity": 1
    }