chuyentt / geojson_vi

An Open-Source Dart and Flutter Library for Efficiently Handling GeoJSON Data in Compliance with RFC 7946
https://pub.dev/packages/geojson_vi
MIT License
15 stars 10 forks source link

Create geojson from string #4

Closed giaur500 closed 3 years ago

giaur500 commented 3 years ago

As I can see, I need to provide json file to create geojson object. Is there any way to create object from string? String contains all the same data, as json file. I just want only operate on strings, not files and all operations need to be performed in memory.

Is it possible? I don't want to save/load from files, because I only need to parse features from geojson string and draw them as polygons/points/lines in google map with Flutter. So, no files operation required.

chuyentt commented 3 years ago

We added a new function "Create GeoJSON from GeoJSON string objects" in version 1.3.3

// Create GeoJSON from GeoJSON String Objects
  var data = jsonEncode(geoJSON.featureCollection.toMap);

  var g = GeoJSON.fromString(data);
  print(g.featureCollection.features.length);
giaur500 commented 3 years ago

Thanks for info, I will try. I wasn't aware of that method, it's not mentioned in readme

giaur500 commented 3 years ago

Ok. It works, but it only accepts FeatureCollection as input. If if try something like this:

 String json="{\n" +
        "    \"type\": \"Point\",\n" +
        "    \"coordinates\": [\n" +
        "        -105.01621,\n" +
        "        39.57422\n" +
        "    ]\n" +
        "}";

I am getting empty GeoJson object. GeoJson specification does not require FeatureCollection, I can put directly any single feature.. Ugly workkarond - I need to check if that's feature collection or not, if not, pack it into FeatureCollection. But, fromString method should do that actually.

Good parser for testing: https://geojsonlint.com/ if you try to validete my example., it renders correctly

chuyentt commented 3 years ago

Ok. It works, but it only accepts FeatureCollection as input. If if try something like this:

 String json="{\n" +
        "    \"type\": \"Point\",\n" +
        "    \"coordinates\": [\n" +
        "        -105.01621,\n" +
        "        39.57422\n" +
        "    ]\n" +
        "}";

I am getting empty GeoJson object. GeoJson specification does not require FeatureCollection, I can put directly any single feature.. Ugly workkarond - I need to check if that's feature collection or not, if not, pack it into FeatureCollection. But, fromString method should do that actually.

Good parser for testing: https://geojsonlint.com/ if you try to validete my example., it renders correctly

We just released geojson_vi 1.4.0 supported: FeatureCollection, Feature and all the Geometries like Point, MultiPoint, LineString, MultiLineString, Polygon, MultiPolygon and GeometryCollection string

giaur500 commented 3 years ago

Excellent, it works. Last question - I need opposite method toString to save gojson as string. There is toString method, but it does not do what I've expected, because it's not implemented and that's generic Object.toString. My suggestion is to implement toString or asString (if, from some reason, you still need generic toString) method. I guess, currently I need to use jsonEncode to get json string and that's the only way?

chuyentt commented 3 years ago

Excellent, it works. Last question - I need opposite method toString to save gojson as string. There is toString method, but it does not do what I've expected, because it's not implemented and that's generic Object.toString. My suggestion is to implement toString or asString (if, from some reason, you still need generic toString) method. I guess, currently I need to use jsonEncode to get json string and that's the only way?

We just released geojson_vi 1.5.0 supported: Added toString() to save GeoJSON objects as string

giaur500 commented 3 years ago

Ok it works. As I can see .toString() method has been added to feature collection (not to GeoJSON).

giaur500 commented 3 years ago

Have you removed fromString method? Not available in version 2. As well as featureColleection field. What is alterntaive now? I need to load geojson from string and enumarate its features.