mapshaper icon indicating copy to clipboard operation
mapshaper copied to clipboard

Possible to return -info in applyCommands?

Open jaltin opened this issue 2 years ago • 5 comments

We are using applyCommands in a nodejs app to convert shapefiles to geojson as per this example code (example shown at https://github.com/mbloch/mapshaper/wiki/Using-mapshaper-programmatically#applycommands):

`// Example: converting a CSV string to GeoJSON const input = {'input.csv': 'lat,lng,value\n40.3,-72.3,1000'}; const cmd = '-i input.csv -points x=lng y=lat -o output.geojson';

// using callback mapshaper.applyCommands(cmd, input, function(err, output) { // do something with {"output.geojson": <Buffer>} }); `

This works really well and gives the converted data in the output object.

It would be very helpful if, in addition to the converted data, it was also possible to get the same data that is provided when passing '-info' in when using mapshaper on command line to the output object.

Is this possible at the moment?

If not, would it be possible to add?

Many thanks!

jaltin avatar Apr 14 '22 13:04 jaltin

Hi @mbloch,

Is this something that is doable and not too hard? We looked a bit at the code and was hard for us to understand how difficult it would be to add as a PR.

jaltin avatar Jun 27 '22 12:06 jaltin

Hi again @mbloch. Pinging you on this issue hoping that you get a chance to given your input.

Hopefully you have an idea if this is feasible to add and if the performance would badly hit by adding that data in the output object? Thanks!

jaltin avatar Sep 02 '22 09:09 jaltin

Hi!

I would like to add the ability to export metadata about data layers. I imagine that most users would want JSON-formatted output.

The main stumbling block for me is deciding what syntax to use. I could add an option named "save-as":-info save-as=info.json. The name "save-as" is already used by the -classify command in a different sense, so maybe output-as= would be better? I'm open to suggestions.

mbloch avatar Sep 02 '22 10:09 mbloch

Thanks for your feedback @mbloch!

Yes we would definitely want that data as JSON so that we can interpret it reliably.

How about 'info-save-as', e.g. "-info info-save-as=metadata.json"?

That way it is very clear that the option is related to -info command.

And if one follows that principle, then also the existing save-as option name could be renamed to classify-save-as?

jaltin avatar Sep 05 '22 09:09 jaltin

The same output as Mapshaper produced by GDAL https://gist.github.com/ThomasG77/725ddcd24a42227a196e0cedf28ad1cc It's then possible to wait for an implementation directly in mapshaper or creating your own metadata.json file now. You may use an output similar to fio cli command in Python https://fiona.readthedocs.io/en/latest/cli.html#info but adding the first feature as an example (more or less what mapshaper -info command does)

FYI, it seems the geometry type from Mapshaper -info output is not always right e.g https://gist.github.com/ThomasG77/725ddcd24a42227a196e0cedf28ad1cc#file-index-js-L51-L53

ThomasG77 avatar Sep 12 '22 22:09 ThomasG77

Still interested in having this added.

@mbloch should I leave this open in case it ever get's addressed, or do I close it as it is out of scope?

jaltin avatar Jan 17 '23 10:01 jaltin

Hi, I added my first stab at this feature to a new release. Now, if you run info with the save-to=<filename> option, you'll get a JSON file with the same information that you see in the -info display. You will get an array of objects, one per targetted layer. For example:

[
  {
    "layer_name": "TOWNSSURVEY_ARC",
    "geometry_type": "polyline",
    "feature_count": 2589,
    "null_shape_count": 2,
    "null_data_count": 0,
    "bbox": [
      33863.73250000179,
      777606.3746000007,
      330836.9724999964,
      959743.0445999987
    ],
    "proj4": "+proj=lcc +x_0=200000 +y_0=750000 +lon_0=-71.5 +lat_1=41.71666666666667 +lat_2=42.68333333333333 +lat_0=41 +datum=NAD83",
    "source_file": "TOWNSSURVEY_ARC.shp",
    "attribute_data": [
      {
        "field": "BND_QUAL",
        "first_value": 0
      },
      {
        "field": "BND_UNCLAS",
        "first_value": 0
      },
      {
        "field": "FINISHED",
        "first_value": "YES"
      },
      {
        "field": "OUTLINE",
        "first_value": 4
      },
      {
        "field": "REASON",
        "first_value": "Not Applicable"
      },
      {
        "field": "SHAPE_LEN",
        "first_value": 96093.8744444
      }
    ]
  },
  {
    "layer_name": "TOWNSSURVEY_POLY",
    "geometry_type": "polygon",
    "feature_count": 1239,
    "null_shape_count": 0,
    "null_data_count": 0,
    "bbox": [
      33863.73250000179,
      777606.3746000007,
      330836.9724999964,
      959743.0445999987
    ],
    "proj4": "+proj=lcc +x_0=200000 +y_0=750000 +lon_0=-71.5 +lat_1=41.71666666666667 +lat_2=42.68333333333333 +lat_0=41 +datum=NAD83",
    "source_file": "TOWNSSURVEY_ARC.shp",
    "attribute_data": [
      {
        "field": "ACRES",
        "first_value": 0.942
      },
      {
        "field": "CCD_MCD",
        "first_value": "100"
      },
      {
        "field": "COASTAL_PO",
        "first_value": "NO"
      },
      {
        "field": "FIPS_COUNT",
        "first_value": 5
      },
      {
        "field": "FIPS_MCD",
        "first_value": 25005100
      },
      {
        "field": "FIPS_PLACE",
        "first_value": "77570"
      },
      {
        "field": "FIPS_STCO",
        "first_value": 25005
      },
      {
        "field": "FOURCOLOR",
        "first_value": 1
      },
      {
        "field": "ISLAND",
        "first_value": 1
      },
      {
        "field": "POP1980",
        "first_value": 13861
      },
      {
        "field": "POP1990",
        "first_value": 13852
      },
      {
        "field": "POP2000",
        "first_value": 14183
      },
      {
        "field": "POP2010",
        "first_value": 15532
      },
      {
        "field": "POPCH00_10",
        "first_value": 1349
      },
      {
        "field": "POPCH80_90",
        "first_value": -9
      },
      {
        "field": "POPCH90_00",
        "first_value": 331
      },
      {
        "field": "SHAPE_AREA",
        "first_value": 3810.9343
      },
      {
        "field": "SHAPE_LEN",
        "first_value": 310.959400383
      },
      {
        "field": "SQUARE_MIL",
        "first_value": 0.002
      },
      {
        "field": "TOWN",
        "first_value": "WESTPORT"
      },
      {
        "field": "TOWN_ID",
        "first_value": 334
      },
      {
        "field": "TYPE",
        "first_value": "T"
      }
    ]
  }
]

mbloch avatar Jan 17 '23 19:01 mbloch

Thanks for quick response @mbloch!

Looks good. Would that also work with applyCommands, so that we can get the JSON back as a callback rather than it being saved to file?

jaltin avatar Jan 18 '23 11:01 jaltin

Yes. The tests I added to test/info-test.mjs use applyCommands to get the JSON.

mbloch avatar Jan 18 '23 12:01 mbloch

Thats great! I will make sure to test it in an upcoming release. Thanks so much for your hard work on mapshaper!

jaltin avatar Jan 18 '23 13:01 jaltin