Skip to content

Instantly share code, notes, and snippets.

View ymattu's full-sized avatar

Yuya MATSUMURA ymattu

View GitHub Profile
---
title: "R で音声入りのスライドを作る"
author: "@y__mattu"
date: "データ解析備忘録"
output:
xaringan::moon_reader:
css: "for_xaringan.css"
nature:
highlightStyle: github
highlightLines: true
@ymattu
ymattu / reversegeo.py
Last active September 11, 2017 01:06
逆ジオコーディング(Python)
import geopandas as gpd
from geopandas.geoseries import *
def reverse_geo(shdf, lon, lat):
"""
逆ジオコーディング
shdfは、geopandasでshapefileを読み込んだデータフレーム
lon, latは経度、緯度
"""
polygon = shdf['geometry']
# http://www.fttsus.jp/worldgrids/ja/our_library/
# calculate 3rd mesh code
cal_meshcode <- function(latitude, longitude){
return(cal_meshcode3(latitude,longitude))
}
# calculate 1st mesh code
cal_meshcode1 <- function(latitude, longitude){
if(latitude < 0){
o <- 4
@ymattu
ymattu / mecab_wakati2.R
Last active August 23, 2017 05:06
mecabで単純な分かち書き
#' @param ... see RMeCab::RMeCabC()
#' @param pos 品詞(正規表現)
mecab_wakati2 <- function(..., pos = "") {
res <- RMeCabC(...) %>%
map(function(x) {
if(str_detect(names(x), pos) == TRUE) {
return(x[[1]])
} else {
""
}
#include <Rcpp.h>
#include <mecab.h>
using namespace Rcpp;
using namespace MeCab;
// [[Rcpp::export]]
Rcpp::DataFrame executeMecab(std::string str, std::string tagger_opt) {
std::vector<std::string> surface, feature;
@ymattu
ymattu / reversegeo.R
Last active August 14, 2017 05:50
緯度経度から逆ジオコーディング(ちょっと高速版)
library(tidyverse)
library(sf)
# 関数
## 都道府県抽出
#' @param sp_polygon object of class sf, sfc or sfg
#' @param lon longitude
#' @param lat latitude
find_pref <- function(sp_polygon, lon, lat) {
# MeCabの{Rcpp}ラッパー定義
Sys.setenv("PKG_LIBS" = "-lmecab")
callRcppMecab <- Rcpp::cppFunction(
code = '
Rcpp::DataFrame executeMecab(std::string str, std::string tagger_opt) {
using namespace Rcpp;
using namespace MeCab;
std::vector<std::string> surface, feature;
MeCab::Tagger *tagger = MeCab::createTagger(tagger_opt.c_str());
const MeCab::Node *node(tagger->parseToNode(str.c_str()));
library(RSelenium)
library(sf)
library(magrittr)
library(tidyverse)
download_smallshape <- function(pref, city, download_dir = "~/Downloads") {
rD <- rsDriver(verbose = FALSE) # Connecting to remote server
remDr <- rD[["client"]]
remDr$navigate("http://e-stat.go.jp/SG2/eStatGIS/page/download.html")
@ymattu
ymattu / latlong2city.R
Created July 17, 2017 16:34
緯度経度から住所を求める関数
# 住所(市)判定
#' @param sp_polygon object of class sf, sfc or sfg
#' @param lon longitude
#' @param lat latitude
find_city <- function(sp_polygon = df, lon = lon, lat = lat) {
which.row <- sf::st_contains(sp_polygon, sf::st_point(c(lon, lat)), sparse = FALSE) %>%
grep(TRUE, .)
if (identical(which.row, integer(0)) == TRUE) {
options cmplib = sasfunc.functions;
data foo;
input customer_number lat long;
datalines;
1 35.039200 135.728965 /* 金閣寺 */
2 35.626065 139.884678 /* ディズニーシー */
3 35.649114 139.742946 /* 慶應義塾大学 */
;
run;