<commit_before>//https://github.com/jasondavies/conrec.js
//http://stackoverflow.com/questions/263305/drawing-a-topographical-map
var g = {}
var _ = require('lodash'),
    fs = require('fs')
    tin = require('./tin'),
    inside = require('./inside'),
    grid = require('./grid'),
    extent = require('./extent'),
    planepoint = require('./planepoint'),
    featurecollection = require('./featurecollection'),
    polygon = require('./polygon')
g.tin = tin
g.inside = inside
g.grid = grid
g.extent = extent
g.planepoint = planepoint
g.featurecollection = featurecollection
g.polygon = polygon

module.exports = function(points, z, resolution, breaks, done){
  g.tin(points, z, function(err, tinResult){
    g.extent(points, function(err, bbox){
      g.grid(bbox, resolution, function(err, gridResult){
        var data = []
        _(gridResult.features).each(function(pt){
          _(tinResult.features).each(function(triangle){
            g.inside(pt, triangle, function(err, isInside){
              if(isInside){
                g.planepoint(pt, triangle, function(err, zValue){
                  pt.properties = {elevation: zValue}
                })
              }
              else {
                //leave pt.properties null
              }
            })
          })
        })
        //fs.writeFileSync('../test/testOut/gridTest.geojson',JSON.stringify(gridResult))
        //fs.writeFileSync('../test/testOut/tinTest.geojson',JSON.stringify(tinResult))

        var depth = Math.sqrt(gridResult.features.length)
        for (var x=0; x<depth; x++){
          var xGroup = gridResult.features.slice(x * depth, (x + 1) * depth)
          var xFlat = []
          _.each(xGroup, function(verticalPoint){
            if(verticalPoint.properties){
              xFlat.push(verticalPoint.properties[z])
            } else{
              xFlat.push(0)
            }
          })
          data.push(xFlat)
        }
        var interval = (bbox[2] - bbox[0]) / depth
        var xCoordinates = []
        var yCoordinates = []
        for (var x=0; x<depth; x++){
          //console.log(x * interval + bbox[0])
          //console.log(x * interval + bbox[1])
          xCoordinates.push(x * interval + bbox[0])
          yCoordinates.push(x * interval + bbox[1])
        }
        
        var c = new Conrec
        c.contour(data, 0, resolution, 0, resolution, xCoordinates, yCoordinates, breaks.length, breaks)
        var contourList = c.contourList()


        var fc = g.featurecollection([])
        _.each(contourList, function(c){
          if(c.length > 2){
            var polyCoordinates = []
            _.each(c, function(coord){
              polyCoordinates.push([coord.x, coord.y])
            })
            var poly = g.polygon([polyCoordinates])
            poly.properties = {}
            poly.properties[z] = c.level

            fc.features.push(poly)
          }
        })
        fs.writeFileSync('../test/testOut/contour.geojson',JSON.stringify(fc))
        done(null, fc)
      })
    })
  })
}


/**
 * Copyright (c) 2010, <NAME>.
 *
 * All rights reserved.  This code is based on Bradley White's Java version,
 * which is in turn based on Nicholas Yue's C++ version, which in turn is based
 * on <NAME>'s original Fortran version.  See below for the respective
 * copyright notices.
 *
 * See http://local.wasp.uwa.edu.au/~pbourke/papers/conrec/ for the original
 * paper by <NAME>.
 *
 * The vector conversion code is based on http://apptree.net/conrec.htm by
 * <NAME>.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions are met:
 *     * Redistributions of source code must retain the above copyright
 *       notice, this list of conditions and the following disclaimer.
 *     * Redistributions in binary form must reproduce the above copyright
 *       notice, this list of conditions and the following disclaimer in the
 *       documentation and/or other materials provided with the distribution.
 *     * Neither the name of the <organization> nor the
 *       names of its contributors may be used to endorse or promote products<commit_msg>took out file write in lib<commit_after>//https://github.com/jasondavies/conrec.js
//http://stackoverflow.com/questions/263305/drawing-a-topographical-map
var g = {}
var _ = require('lodash'),
    fs = require('fs')
    tin = require('./tin'),
    inside = require('./inside'),
    grid = require('./grid'),
    extent = require('./extent'),
    planepoint = require('./planepoint'),
    featurecollection = require('./featurecollection'),
    polygon = require('./polygon')
g.tin = tin
g.inside = inside
g.grid = grid
g.extent = extent
g.planepoint = planepoint
g.featurecollection = featurecollection
g.polygon = polygon

module.exports = function(points, z, resolution, breaks, done){
  g.tin(points, z, function(err, tinResult){
    g.extent(points, function(err, bbox){
      g.grid(bbox, resolution, function(err, gridResult){
        var data = []
        _(gridResult.features).each(function(pt){
          _(tinResult.features).each(function(triangle){
            g.inside(pt, triangle, function(err, isInside){
              if(isInside){
                g.planepoint(pt, triangle, function(err, zValue){
                  pt.properties = {elevation: zValue}
                })
              }
              else {
                //leave pt.properties null
              }
            })
          })
        })
        //fs.writeFileSync('../test/testOut/gridTest.geojson',JSON.stringify(gridResult))
        //fs.writeFileSync('../test/testOut/tinTest.geojson',JSON.stringify(tinResult))

        var depth = Math.sqrt(gridResult.features.length)
        for (var x=0; x<depth; x++){
          var xGroup = gridResult.features.slice(x * depth, (x + 1) * depth)
          var xFlat = []
          _.each(xGroup, function(verticalPoint){
            if(verticalPoint.properties){
              xFlat.push(verticalPoint.properties[z])
            } else{
              xFlat.push(0)
            }
          })
          data.push(xFlat)
        }
        var interval = (bbox[2] - bbox[0]) / depth
        var xCoordinates = []
        var yCoordinates = []
        for (var x=0; x<depth; x++){
          //console.log(x * interval + bbox[0])
          //console.log(x * interval + bbox[1])
          xCoordinates.push(x * interval + bbox[0])
          yCoordinates.push(x * interval + bbox[1])
        }
        
        var c = new Conrec
        c.contour(data, 0, resolution, 0, resolution, xCoordinates, yCoordinates, breaks.length, breaks)
        var contourList = c.contourList()


        var fc = g.featurecollection([])
        _.each(contourList, function(c){
          if(c.length > 2){
            var polyCoordinates = []
            _.each(c, function(coord){
              polyCoordinates.push([coord.x, coord.y])
            })
            var poly = g.polygon([polyCoordinates])
            poly.properties = {}
            poly.properties[z] = c.level

            fc.features.push(poly)
          }
        })
        //fs.writeFileSync('../test/testOut/contour.geojson',JSON.stringify(fc))
        done(null, fc)
      })
    })
  })
}


/**
 * Copyright (c) 2010, <NAME>.
 *
 * All rights reserved.  This code is based on Bradley White's Java version,
 * which is in turn based on Nicholas Yue's C++ version, which in turn is based
 * on <NAME>'s original Fortran version.  See below for the respective
 * copyright notices.
 *
 * See http://local.wasp.uwa.edu.au/~pbourke/papers/conrec/ for the original
 * paper by <NAME>.
 *
 * The vector conversion code is based on http://apptree.net/conrec.htm by
 * <NAME>.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions are met:
 *     * Redistributions of source code must retain the above copyright
 *       notice, this list of conditions and the following disclaimer.
 *     * Redistributions in binary form must reproduce the above copyright
 *       notice, this list of conditions and the following disclaimer in the
 *       documentation and/or other materials provided with the distribution.
 *     * Neither the name of the <organization> nor the
 *       names of its contributors may be used to endorse or promote products
{'fraction_non_alphanumeric': 0.1250446588067167, 'fraction_numerical': 0.005478146957246636, 'mean_word_length': 2.2374710871241326, 'pattern_counts': {'":': 0, '<': 17, '<?xml version=': 0, '>': 15, 'https://': 2, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '5781272', 'n_tokens_mistral': 2462, 'n_tokens_neox': 2362, 'n_words': 628}