Add a way to export the results from the -calc command
The -calc command currently just logs its output to the console. If you run a command like -calc 'sum(pop)' you get console output that looks like like:
[calc] sum(pop): 812
-calc might be more useful if we could save the output to a file.
One way would be to send the output from -calc to a data layer, and use variable assignments to set the name of the calculated data fields, similar to how the -each command works. So -calc 'tot_pop = sum(pop)' would create a layer containing a single data record, and the layer would look like [{"tot_pop": 812}] in JSON format, or tot_pop\n812 in CSV format. This is consistent with how other mapshaper commands work.
A more complete example:
mapshaper input.json -calc 'tot_pop = sum(pop)' -o output.json format=json
I'm open to suggestions for other ways to perform calculations and output the results.
@mbloch is this already implement or still a suggestion? BTW I guess it's a nice way to output the useful -calc command like that
This is still an open issue. I'd like to come up with a more useful way to display summary statistics about a dataset. What should the command syntax be? How should the output be formatted? These are open questions.
For the json file, I guess it will be interesting to have an statistic input before all the other datas like
{"type":"FeatureCollection",
{
"stats (or calc)": {
"tot_pop": " sum(pop)",
"mean_pop": "mean(pop",
...
},
"features": []
}
For the command syntax maybe it will be great to have an option for the -o command like calc or stats in order to not be confusing with the -calc command
-o output.json format=json calc
But I'm not an expert in syntax ;)
I'm against outputting non-standard GeoJSON, or adding more options to the -o command.
An alternative would be adding a command for generating multiple statistics about one or more data fields, and either printing the output to the console (like the -info command) or sending the output to a new data layer that could be saved as a CSV file or array of JSON records.
Indeed it's not a good idea to break the geojson format you're right. So the option of the second file look like to be the best idea.
Can we will think about a new format=jsoncalc or format=csvcalc which create one file with the datas and a second one with the output of the calc console result. So you're still free to do all the math you want like this:
-calc 'sum(Log_av45)+sum(Log_45_70)+sum(Log_ap90)+sum(Log_inc)' -calc 'sum(Log_av45)' -calc 'sum(Log_45_70)' -calc 'sum(Log_ap90)' -calc 'sum(Men_surf)/(sum(Log_av45)+sum(Log_45_70)+sum(Log_ap90)+sum(Log_inc))'
@mbloch do you have a temp solution for getting the output of the console in a php var for example?