How to use the turf.polygon function in turf

To help you get started, we’ve selected a few turf examples, based on popular ways it is used in public projects.

Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.

github kepta / idly / src / map / utils / wayToFeat.ts View on Github external
let feat;
    if (w.geometry === Geometry.LINE) {
      // if (!n.loc) throw new Error('doesnt have loc');
      feat = turf.lineString(
        nodes.map(n => [n.loc.lon, n.loc.lat]).toArray(),
        properties
      ) as Feature;
      feat.id = w.id;
    } else if (w.geometry === Geometry.AREA) {
      // if (nodes[0])
      if (!nodes.first().equals(nodes.last()))
        throw new Error('area first last not same');
      const geoCoordinates = nodes.map(n => [n.loc.lon, n.loc.lat]).toArray();
      // if (R.equals())
      // geoCoordinates.push(geoCoordinates[0]);
      feat = turf.polygon([geoCoordinates], properties) as Feature<
        Polygon,
        IWayProperties
      >;
    }
    feat.id = w.id;
    return feat;
  }
}
github mapbox / tile-reduce / test / trace / trace.js View on Github external
lines.features = lines.features.map(function(line){
      try {
        var clipped = turf.intersect(line, turf.polygon(tilebelt.tileToGeoJSON(tile).coordinates));
        return clipped;
      } catch(e){
        return;
      }
    });
    lines.features = lines.features.filter(function(line){
github mapbox / timespace / util / map.js View on Github external
Object.keys(timezones).forEach(function(timezone){
  var poly = turf.polygon(tilebelt.tileToGeoJSON(timezone.split('/').map(Number)).coordinates);
  poly.properties.zone = timezones[timezone]
  console.log(JSON.stringify(poly))
});
github mapbox / timespace / regenerate / quantize.js View on Github external
cover.tiles(zone.geometry, opts).forEach(function(tile) {
    var id = tile.join('/');
    var poly = turf.polygon(tilebelt.tileToGeoJSON(tile).coordinates);

    try {
      var overlap = turf.area(turf.intersect(zone, poly));
      if (!tiles[id] || tiles[id].overlap < overlap)
        tiles[id] = {name: zone.properties.tzid, overlap: overlap};
    } catch (e) {
      console.log('Error detected: ' + e.message + '; zone: ' + zone.properties.tzid + '; tile: ' + id);
    }

  });
github jpwright / subway / tools / demanderator.js View on Github external
var dim = [];
                for (var b = 0; b < voxels_dim; b++) {
                    dim.push(0.0);
                }
                demand.push(dim);
            }
            
            var landmark_polygons_of_interest = {"JFK Airport": 0, "LaGuardia Airport": 0, "Grand Central": 0, "Port Authority Bus Terminal": 0, "Penn Station": 0};
            var landmark_scalers = {"JFK Airport": 11000.0, "LaGuardia Airport": 9000.0, "Grand Central": 2000.0, "Port Authority Bus Terminal": 1000.0, "Penn Station": 2000.0};
            
            for (landmark_index = 0; landmark_index < landmarks_data["features"].length; landmark_index++) {
                var landmark = landmarks_data["features"][landmark_index];
                var landmark_name = landmark["properties"]["name"];
                if (landmark_name in landmark_polygons_of_interest) {
                    //console.log(landmark["geometry"]["coordinates"]);
                    var landmark_polygon = turf.polygon(landmark["geometry"]["coordinates"]);
                    landmark_polygons_of_interest[landmark_name] = landmark_polygon;
                    var landmark_centroid = turf.centroid(landmark_polygon);
                    console.log("Landmark "+landmark_name+" centroid in polygon: "+turf.inside(landmark_centroid, landmark_polygon));
                }
            }
            
            
            console.log("Calculating centroids");
            var centroids = [];
            for (tract_index = 0; tract_index < data.features.length; tract_index++) {
                var turf_centroid = turf.centroid(data.features[tract_index]);
                //var tract_area = turf.area(turf_polygons[tract_index]);
                centroids[tract_index] = turf_centroid;
            }
            
            console.log("Calculating voxels");
github polygon-city / polygon-city-cli / workers / geojsonIndex.js View on Github external
var point = turf.point(origin, properties);

  if (!groundSurfaces || groundSurfaces.length === 0) {
    return point;
  }

  try {
    var points;
    var polygons = [];
    for (var i = 0; i < groundSurfaces.length; i++) {
      points = citygmlPoints(groundSurfaces[i]).map((point) => {
        return proj4('EPSG:ORIGIN').inverse([point[0], point[1]]);
      });

      polygons.push(turf.polygon([points], properties));
    }

    var featureCollection = turf.featurecollection(polygons);
    var polygon = turf.merge(featureCollection);

    return polygon;
  } catch(err) {
    console.error(chalk.red(err));
    return point;
  }
};
github mapbox / cheap-ruler / bench / bench-area.js View on Github external
'turf.area': function () {
        for (var i = 0; i < polygons.length; i++) {
            turf.area(turf.polygon(polygons[i]));
        }
    },
    'ruler.area': function () {
github hotosm / osm-analytics / app / components / Map / regionToCoords.js View on Github external
type: 'Feature',
          properties: {},
          geometry: geometry
        }
      }
    }).catch(function(err) {
      if (err.status == 404) {
        throw new Error('unknown hot project', projectId)
      } else {
        throw err
      }
    });
  } else if (region.type === 'bbox') {
    coords = bboxPolygon(region.coords)
  } else if (region.type === 'polygon') {
    coords = polygon([region.coords.concat([region.coords[0]])])
  } else {
    throw new Error('unknown region', region)
  }
  return Promise.resolve(coords).then(function(coords) {
    if (latLngOrder) {
      return flip(coords)
    } else {
      return coords
    }
  })
}
github kepta / idly / src / map / highPerf / way.ts View on Github external
export function wayToLineString(geometry: Geometry, nodeCoords: number[][]) {
  let feat;
  if (geometry === Geometry.LINE) {
    feat = turf.lineString(nodeCoords, {}) as Feature<
      LineString,
      IWayProperties
    >;
  } else if (geometry === Geometry.AREA) {
    feat = turf.polygon([nodeCoords], {}) as Feature;
  } else {
    throw new Error('not a matching geometry provided for way');
  }
  return feat;
}
github probr / probr-analysis / worker / components / location / location.foreach.js View on Github external
function locationToCircle(loc, multiplier) {
      var point = turf.point([loc.long,loc.lat]);
      var distance = signalToMeters(loc.weightedSignal, multiplier);

      var coordinates = [];
      for (var i=0; i<=360; i+=10) {
        coordinates.push( turf.destination(point, distance/1000, i, 'kilometers').geometry.coordinates );
      }
      var poly = turf.polygon([coordinates]);
      return poly;
    }