Пример #1
0
var processFile = function (d) {
  process.stdout.write(checkpoint + '\r');
  checkpoint++;
  var lat, long, addr;
  var tmp = d.Tran_Location.split('(');
  addr = tmp[0];
  if (!tmp[1]) return;
  lat  = tmp[1].split(',')[0].trim();
  if (!tmp[1].split(',')[1]) return;
  long = tmp[1].split(',')[1].slice(0, -1).trim();
  // console.log(addr + ' | ' + lat + ' | ' + long);
  // if (checkpoint > 10) return;
  var i, district, state, censusTract, county;
  d.lat = lat || '';
  d.long = long || '';
  d.zip = !!addr.match(zipRegex) ? addr.match(zipRegex)[0].trim() : '';
  d.district = '';
  d.state = 'CA';
  d.Census_Tract = '';
  d.county = '';

  for (i = 0; i < districts.features.length; i++) {
    district = districts.features[i];
    if (geo.pointInPolygon({type:'Point', coordinates: [long, lat]}, district.geometry)) {
      // console.log(lat, long, JSON.stringify(district.geometry.coordinates[0][0]));
      d.district = district.properties.DISTRICT;
      break;
    }
  };
  if (d.district === '') {
    for (i = 0; i < states.features.length; i++) {
      state = states.features[i];
      if (geo.pointInPolygon({type:'Point', coordinates: [long, lat]}, state.geometry)) {
        d.state = state.properties.STATE;
        break;
      }
    };
  }
  if (d.state !== 'CA') { csvStream.write(d); return; }
  for (i = 0; i < censusTracts.features.length; i++) {
    censusTract = censusTracts.features[i];
    if (geo.pointInPolygon({type:'Point', coordinates: [long, lat]}, censusTract.geometry)) {
      d.Census_Tract = censusTract.properties.BLOCKCE10;
      break;
    }
  };
  for (i = 0; i < counties.features.length; i++) {
    county = counties.features[i];
    if (geo.pointInPolygon({type:'Point', coordinates: [long, lat]}, county.geometry)) {
      d.county = county.properties.NAME;
      break;
    }
  };
  csvStream.write(d);
};
Пример #2
0
  neighborhoods.features.forEach(function(hood){
    //check first point in route
    if ( gju.pointInPolygon( turf.point(route.geometry.coordinates[0]).geometry, hood.geometry) )
      route.properties.hoods.push(hood.properties.name);
    //check last point in route
    if ( gju.pointInPolygon( turf.point(route.geometry.coordinates[route.geometry.coordinates.length -1]).geometry, hood.geometry) )
      route.properties.hoods.push(hood.properties.name);

    // check if hood and bikeroute intersect
    var templine = {"type":"LineString", "coordinates": hood.geometry.coordinates[0]}; // gju.lineStringsIntersect only works with "LineString", not "Polygon"
    if ( gju.lineStringsIntersect( templine, route.geometry) )
      route.properties.hoods.push(hood.properties.name);
  })
Пример #3
0
function checkPointInPolygon(location, coordinates, cb){
    // returns true if point is in polygon, with result then wrapped in function for callback
    if (gju.pointInPolygon({"type":"Point","coordinates":location}, {"type":"Polygon", "coordinates": coordinates}))
        { return cb(null, true); }
        else { err = new Error("No polygon found")
        return cb(err)}
};
Пример #4
0
 layer.eachLayer(function(l) {
     if (first && results.length) return;
     var geom = leafletPip.getGeometry(l);
     if (geom && gju.pointInPolygon({ type: 'Point', coordinates: p }, geom)) {
         results.push(l);
     }
 });
Пример #5
0
var data_accidents = data_accidents_2010_2013.concat(data_accidents_2014).map(function(d) {

    var obj1 = _accidentType(d[ 'Object 1' ]);
    var obj2 = _accidentType(d[ 'Object 2' ]);

    // Build the accident data object.
    var obj = {
        date: moment.utc(d[ 'Date Time' ], 'MM/DD/YYYY HH:mm:ss A').toDate(),
        coordinates: [ parseFloat(d[ 'Longitude' ]), parseFloat(d[ 'Latitude' ]) ],
        streetName: d[ 'Steet Name' || 'Street Name' ] || null,
        crossStreet: d[ 'Cross Street' ] || null,
        neighborhood: null,
        accidentType: obj1 === 'Auto' ? obj2 : (obj2 === 'Auto' ? obj1 : null)
    };

    // Determine in which neighborhood this accident occurred.
    var point = { type: 'Point', coordinates: obj.coordinates };
    for (var i = 0; i < geo_neighborhoods.features.length; i++) {
        var feature = geo_neighborhoods.features[ i ];
        var polygon = feature.geometry;
        if (gju.pointInPolygon(point, polygon)) {
            obj.neighborhood = feature.properties[ 'N_HOOD' ];
            break;
        }
    }
    // Return the accident data object.
    return obj;

// Filter out null accident types.
}).filter(function(d) {
Пример #6
0
 arrayofmulticoordinates.forEach(function(coordinates){
     if (gju.pointInPolygon({"type":"Point","coordinates":location}, {"type":"Polygon", "coordinates": coordinates}))
     { return cb(null, true); }
     else { 
         err = new Error("No polygon found")
         return cb(err)
     }
 })
Пример #7
0
 layer.eachLayer(function(l) {
     if (first && results.length) return;
     if ((l instanceof L.MultiPolygon || l instanceof L.Polygon) && gju.pointInPolygon({
             type: 'Point',
             coordinates: p
         }, l.toGeoJSON().geometry)) {
         results.push(l);
     }
 });
Пример #8
0
function inGeometry(latlng,geometry){
    var point = {"type":"Point",'coordinates':latlng};
    if(geometry.type == 'Polygon'){
        return(gju.pointInPolygon(point,geometry));
    }
    if(geometry.type == 'MultiPolygon'){
        return(gju.pointInMultiPolygon(point,geometry));
    }
    else{
        throw(new Error("Unsupported geometry type"));
    }
}
module.exports = function(points, polygons, options) {
  var pointObjects = points.objects,
      polygonObjects = polygons.objects,
      polygonObjectKey = null,
      countKey = options.key

  for(polygonObjectKey in polygonObjects) {

    if(polygonObjects[polygonObjectKey].type !== 'GeometryCollection')
      continue

    var polygonGeometries = polygonObjects[polygonObjectKey].geometries,
      i = polygonGeometries.length

    while(i--) {
      var polygon = polygonGeometries[i],
          pointObjectKey = null

      // No support for multipolygons just yet
      if(polygon.type === 'MultiPolygon')
        continue

      process.stdout.write("Checking " + i + " polygon...\r")

      polygon = topojson.feature(polygons, polygon)

      for(pointObjectKey in pointObjects) {
        var pointGeometries = pointObjects[pointObjectKey].geometries,
            j = pointGeometries.length

        if(typeof polygon.properties[countKey] === 'undefined')
          polygon.properties[countKey] = 0

        while(j--) {
          var point = topojson.feature(points, pointGeometries[j])

          if(gju.pointInPolygon(point.geometry, polygon.geometry))
            polygon.properties[countKey] += 1
        }
      }

    }
  }

  if(options.out) {
    fs.writeFileSync(path.join(options.out), JSON.stringify(polygons), "utf8");
  } else {
    console.log(polygons)
  }
}
Пример #10
0
 pointInPolygon: function(point,layer){
 	return gju.pointInPolygon(point.toGeoJSON().geometry, layer.toGeoJSON().geometry);
 },
Пример #11
0
!function(f){if("object"==typeof exports&&"undefined"!=typeof module)module.exports=f();else if("function"==typeof define&&define.amd)define([],f);else{var g;g="undefined"!=typeof window?window:"undefined"!=typeof global?global:"undefined"!=typeof self?self:this,g.leafletPip=f()}}(function(){return function e(t,n,r){function s(o,u){if(!n[o]){if(!t[o]){var a="function"==typeof require&&require;if(!u&&a)return a(o,!0);if(i)return i(o,!0);var f=new Error("Cannot find module '"+o+"'");throw f.code="MODULE_NOT_FOUND",f}var l=n[o]={exports:{}};t[o][0].call(l.exports,function(e){var n=t[o][1][e];return s(n?n:e)},l,l.exports,e,t,n,r)}return n[o].exports}for(var i="function"==typeof require&&require,o=0;o<r.length;o++)s(r[o]);return s}({1:[function(require,module){"use strict";function isPoly(l){return L.MultiPolygon?l instanceof L.MultiPolygon||l instanceof L.Polygon:l.feature&&l.feature.geometry&&l.feature.geometry.type&&-1!==["Polygon","MultiPolygon"].indexOf(l.feature.geometry.type)}var gju=require("geojson-utils"),leafletPip={bassackwards:!1,pointInLayer:function(p,layer,first){p instanceof L.LatLng?p=[p.lng,p.lat]:leafletPip.bassackwards&&(p=p.concat().reverse());var results=[];return layer.eachLayer(function(l){first&&results.length||isPoly(l)&&gju.pointInPolygon({type:"Point",coordinates:p},l.toGeoJSON().geometry)&&results.push(l)}),results}};module.exports=leafletPip},{"geojson-utils":2}],2:[function(require,module){!function(){function boundingBoxAroundPolyCoords(coords){for(var xAll=[],yAll=[],i=0;i<coords[0].length;i++)xAll.push(coords[0][i][1]),yAll.push(coords[0][i][0]);return xAll=xAll.sort(function(a,b){return a-b}),yAll=yAll.sort(function(a,b){return a-b}),[[xAll[0],yAll[0]],[xAll[xAll.length-1],yAll[yAll.length-1]]]}function pnpoly(x,y,coords){for(var vert=[[0,0]],i=0;i<coords.length;i++){for(var j=0;j<coords[i].length;j++)vert.push(coords[i][j]);vert.push(coords[i][0]),vert.push([0,0])}for(var inside=!1,i=0,j=vert.length-1;i<vert.length;j=i++)vert[i][0]>y!=vert[j][0]>y&&x<(vert[j][1]-vert[i][1])*(y-vert[i][0])/(vert[j][0]-vert[i][0])+vert[i][1]&&(inside=!inside);return inside}var gju=this.gju={};"undefined"!=typeof module&&module.exports&&(module.exports=gju),gju.lineStringsIntersect=function(l1,l2){for(var intersects=[],i=0;i<=l1.coordinates.length-2;++i)for(var j=0;j<=l2.coordinates.length-2;++j){var a1={x:l1.coordinates[i][1],y:l1.coordinates[i][0]},a2={x:l1.coordinates[i+1][1],y:l1.coordinates[i+1][0]},b1={x:l2.coordinates[j][1],y:l2.coordinates[j][0]},b2={x:l2.coordinates[j+1][1],y:l2.coordinates[j+1][0]},ua_t=(b2.x-b1.x)*(a1.y-b1.y)-(b2.y-b1.y)*(a1.x-b1.x),ub_t=(a2.x-a1.x)*(a1.y-b1.y)-(a2.y-a1.y)*(a1.x-b1.x),u_b=(b2.y-b1.y)*(a2.x-a1.x)-(b2.x-b1.x)*(a2.y-a1.y);if(0!=u_b){var ua=ua_t/u_b,ub=ub_t/u_b;ua>=0&&1>=ua&&ub>=0&&1>=ub&&intersects.push({type:"Point",coordinates:[a1.x+ua*(a2.x-a1.x),a1.y+ua*(a2.y-a1.y)]})}}return 0==intersects.length&&(intersects=!1),intersects},gju.pointInBoundingBox=function(point,bounds){return!(point.coordinates[1]<bounds[0][0]||point.coordinates[1]>bounds[1][0]||point.coordinates[0]<bounds[0][1]||point.coordinates[0]>bounds[1][1])},gju.pointInPolygon=function(p,poly){for(var coords="Polygon"==poly.type?[poly.coordinates]:poly.coordinates,insideBox=!1,i=0;i<coords.length;i++)gju.pointInBoundingBox(p,boundingBoxAroundPolyCoords(coords[i]))&&(insideBox=!0);if(!insideBox)return!1;for(var insidePoly=!1,i=0;i<coords.length;i++)pnpoly(p.coordinates[1],p.coordinates[0],coords[i])&&(insidePoly=!0);return insidePoly},gju.pointInMultiPolygon=function(p,poly){for(var coords_array="MultiPolygon"==poly.type?[poly.coordinates]:poly.coordinates,insideBox=!1,insidePoly=!1,i=0;i<coords_array.length;i++){for(var coords=coords_array[i],j=0;j<coords.length;j++)insideBox||gju.pointInBoundingBox(p,boundingBoxAroundPolyCoords(coords[j]))&&(insideBox=!0);if(!insideBox)return!1;for(var j=0;j<coords.length;j++)insidePoly||pnpoly(p.coordinates[1],p.coordinates[0],coords[j])&&(insidePoly=!0)}return insidePoly},gju.numberToRadius=function(number){return number*Math.PI/180},gju.numberToDegree=function(number){return 180*number/Math.PI},gju.drawCircle=function(radiusInMeters,centerPoint,steps){for(var center=[centerPoint.coordinates[1],centerPoint.coordinates[0]],dist=radiusInMeters/1e3/6371,radCenter=[gju.numberToRadius(center[0]),gju.numberToRadius(center[1])],steps=steps||15,poly=[[center[0],center[1]]],i=0;steps>i;i++){var brng=2*Math.PI*i/steps,lat=Math.asin(Math.sin(radCenter[0])*Math.cos(dist)+Math.cos(radCenter[0])*Math.sin(dist)*Math.cos(brng)),lng=radCenter[1]+Math.atan2(Math.sin(brng)*Math.sin(dist)*Math.cos(radCenter[0]),Math.cos(dist)-Math.sin(radCenter[0])*Math.sin(lat));poly[i]=[],poly[i][1]=gju.numberToDegree(lat),poly[i][0]=gju.numberToDegree(lng)}return{type:"Polygon",coordinates:[poly]}},gju.rectangleCentroid=function(rectangle){var bbox=rectangle.coordinates[0],xmin=bbox[0][0],ymin=bbox[0][1],xmax=bbox[2][0],ymax=bbox[2][1],xwidth=xmax-xmin,ywidth=ymax-ymin;return{type:"Point",coordinates:[xmin+xwidth/2,ymin+ywidth/2]}},gju.pointDistance=function(pt1,pt2){var lon1=pt1.coordinates[0],lat1=pt1.coordinates[1],lon2=pt2.coordinates[0],lat2=pt2.coordinates[1],dLat=gju.numberToRadius(lat2-lat1),dLon=gju.numberToRadius(lon2-lon1),a=Math.pow(Math.sin(dLat/2),2)+Math.cos(gju.numberToRadius(lat1))*Math.cos(gju.numberToRadius(lat2))*Math.pow(Math.sin(dLon/2),2),c=2*Math.atan2(Math.sqrt(a),Math.sqrt(1-a));return 6371*c*1e3},gju.geometryWithinRadius=function(geometry,center,radius){if("Point"==geometry.type)return gju.pointDistance(geometry,center)<=radius;if("LineString"==geometry.type||"Polygon"==geometry.type){var coordinates,point={};coordinates="Polygon"==geometry.type?geometry.coordinates[0]:geometry.coordinates;for(var i in coordinates)if(point.coordinates=coordinates[i],gju.pointDistance(point,center)>radius)return!1}return!0},gju.area=function(polygon){for(var p1,p2,area=0,points=polygon.coordinates[0],j=points.length-1,i=0;i<points.length;j=i++){var p1={x:points[i][1],y:points[i][0]},p2={x:points[j][1],y:points[j][0]};area+=p1.x*p2.y,area-=p1.y*p2.x}return area/=2},gju.centroid=function(polygon){for(var f,p1,p2,x=0,y=0,points=polygon.coordinates[0],j=points.length-1,i=0;i<points.length;j=i++){var p1={x:points[i][1],y:points[i][0]},p2={x:points[j][1],y:points[j][0]};f=p1.x*p2.y-p2.x*p1.y,x+=(p1.x+p2.x)*f,y+=(p1.y+p2.y)*f}return f=6*gju.area(polygon),{type:"Point",coordinates:[y/f,x/f]}},gju.simplify=function(source,kink){kink=kink||20,source=source.map(function(o){return{lng:o.coordinates[0],lat:o.coordinates[1]}});var n_source,n_stack,n_dest,start,end,i,sig,dev_sqr,max_dev_sqr,band_sqr,x12,y12,d12,x13,y13,d13,x23,y23,d23,F=Math.PI/180*.5,index=new Array,sig_start=new Array,sig_end=new Array;if(source.length<3)return source;for(n_source=source.length,band_sqr=360*kink/(2*Math.PI*6378137),band_sqr*=band_sqr,n_dest=0,sig_start[0]=0,sig_end[0]=n_source-1,n_stack=1;n_stack>0;)if(start=sig_start[n_stack-1],end=sig_end[n_stack-1],n_stack--,end-start>1){for(x12=source[end].lng()-source[start].lng(),y12=source[end].lat()-source[start].lat(),Math.abs(x12)>180&&(x12=360-Math.abs(x12)),x12*=Math.cos(F*(source[end].lat()+source[start].lat())),d12=x12*x12+y12*y12,i=start+1,sig=start,max_dev_sqr=-1;end>i;i++)x13=source[i].lng()-source[start].lng(),y13=source[i].lat()-source[start].lat(),Math.abs(x13)>180&&(x13=360-Math.abs(x13)),x13*=Math.cos(F*(source[i].lat()+source[start].lat())),d13=x13*x13+y13*y13,x23=source[i].lng()-source[end].lng(),y23=source[i].lat()-source[end].lat(),Math.abs(x23)>180&&(x23=360-Math.abs(x23)),x23*=Math.cos(F*(source[i].lat()+source[end].lat())),d23=x23*x23+y23*y23,dev_sqr=d13>=d12+d23?d23:d23>=d12+d13?d13:(x13*y12-y13*x12)*(x13*y12-y13*x12)/d12,dev_sqr>max_dev_sqr&&(sig=i,max_dev_sqr=dev_sqr);band_sqr>max_dev_sqr?(index[n_dest]=start,n_dest++):(n_stack++,sig_start[n_stack-1]=sig,sig_end[n_stack-1]=end,n_stack++,sig_start[n_stack-1]=start,sig_end[n_stack-1]=sig)}else index[n_dest]=start,n_dest++;index[n_dest]=n_source-1,n_dest++;for(var r=new Array,i=0;n_dest>i;i++)r.push(source[index[i]]);return r.map(function(o){return{type:"Point",coordinates:[o.lng,o.lat]}})},gju.destinationPoint=function(pt,brng,dist){dist/=6371,brng=gju.numberToRadius(brng);var lon1=gju.numberToRadius(pt.coordinates[0]),lat1=gju.numberToRadius(pt.coordinates[1]),lat2=Math.asin(Math.sin(lat1)*Math.cos(dist)+Math.cos(lat1)*Math.sin(dist)*Math.cos(brng)),lon2=lon1+Math.atan2(Math.sin(brng)*Math.sin(dist)*Math.cos(lat1),Math.cos(dist)-Math.sin(lat1)*Math.sin(lat2));return lon2=(lon2+3*Math.PI)%(2*Math.PI)-Math.PI,{type:"Point",coordinates:[gju.numberToDegree(lon2),gju.numberToDegree(lat2)]}}}()},{}]},{},[1])(1)});