Skip to content

Instantly share code, notes, and snippets.

View maptastik's full-sized avatar

Ryan Cooper maptastik

View GitHub Profile
@maptastik
maptastik / shps_in_dir.py
Last active December 6, 2018 19:17
Function that takes a directory as a parameter and returns a list of shapefiles. Although this doesn't explicitly involve arcpy, this function could be used for automating an action on all shapefiles in a directory using arcpy. That said, it could be
import os
def shps_in_dir(search_directory, full_path = True):
shp_list = []
for file in os.listdir(search_directory):
if file.endswith('.shp'):
shp_list.append(file)
if len(shp_list) > 0 and full_path:
shp_list = list(map(lambda x: os.path.join(search_directory,x), shp_list))
return shp_list
@maptastik
maptastik / allFieldsPopup.js
Last active January 22, 2019 15:11
List all of a features fields and values in a popup in Leaflet
function allFieldsPopup(feature, layer) {
let featureProperties = feature.properties;
let popupHtml = '';
for(let property in featureProperties) {
popupHtml+=`<b>${property}:</b> ${featureProperties[property]}<br>`
}
layer.bindPopup(popupHtml)
}
@maptastik
maptastik / consolidatePolygons.js
Created December 1, 2018 16:11
Consolidate multiple Polygon and MultiPolygon feature geometries into a single MultiPolygon geometry array
function consolidatePolygons(polygonsJson) {
let consolidatedPolygonsArray = [];
let polygonsFeatures = polygonsJson.features;
for (let i = 0; i < polygonsFeatures.length; i++) {
let polygonFeature = polygonsFeatures[i];
let coordinates = polygonFeature.geometry.coordinates;
if (polygonFeature.geometry.type === "MultiPolygon") {
for (let j = 0; j < coordinates.length; j++) {
consolidatedPolygonsArray.push(coordinates[j]);
@maptastik
maptastik / createArrayOfPointCoordinates.js
Created December 1, 2018 15:49
Sometimes you just need the point coordinates for all the points in a GeoJSON. This function pulls them into an array.
function createArrayOfPointCoordinates(pointsJson) {
let pointsCoordinatesArray = [];
let pointsFeatures = pointsJson.features;
for (let i = 0; i < pointsFeatures.length; i++) {
let pointsFeature = pointsFeatures[i];
if (pointsFeature.geometry != null) {
let pointsFeaturesCoordinates = pointsFeature.geometry.coordinates;
pointsCoordinatesArray.push(pointsFeaturesCoordinates);
}
@maptastik
maptastik / countPointsInPolygons.js
Last active December 1, 2018 16:03
Iterate through polygon geojson and find number of points in each
function countPointsInPolygons(points, polygonJson) {
let countsArray = [];
let polygonFeaturesArray = polygonJson.features;
for (let i = 0; i < polygonFeaturesArray.length; i++) {
let iGeometry = turf.polygon(polygonFeaturesArray[i].geometry.coordinates);
let pointsWithinPolygon = turf.pointsWithinPolygon(points, iGeometry);
iGeometry.properties.count = pointsWithinPolygon.features.length;
countsArray.push(iGeometry);
}
return turf.featureCollection(countsArray);
@maptastik
maptastik / README.md
Last active December 1, 2018 15:28
This function takes an input GeoJSON dataset and creates a generates a hex grid from the bounding box of that layer.

This function takes an input GeoJSON dataset and creates a generates a hex grid from the bounding box of that layer. Because of the way hex grids are created using turfjs, using the bounding box doesn't necessarily result in every area of the input GeoJSON being covered by a hex grid. As such you can also set buffer parameters, radius and bufferUnits, to expand the area for which the bounding box is created. You can define the size of the hexagons with the cellSide and hexUnits parameters.

@maptastik
maptastik / Lenox_Parks_Network_Test.ipynb
Last active October 26, 2018 19:59
osmnx test on parks in Lenox, MA
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
@maptastik
maptastik / in_state.geojson
Created October 24, 2018 00:14
MAP671, Lab 2 Challenge: Indiana state boundary
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
@maptastik
maptastik / lenox_pap.geojson
Created October 19, 2018 21:15
Lenox Parks Dummy Data
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.