I tried to get the distance from the address string to the nearest station with ruby

I tried to get the information of the nearest station from the address with ruby.

Roughly what I did

  1. Get latitude / longitude from address with Yahoo API
  2. Get the nearest station information from latitude and longitude with Heart Rails API

Execution environment

  1. ruby2.7.2
  2. mac catalina
  3. Execution date and time 2021/01/01

1. Get latitude / longitude from address with Yahoo API

1.1 Get yahoo app ID

Get here https://e.developer.yahoo.co.jp/dashboard/

1.2 Request

-Encode the requested address to url ・ Request with open-uri The response looks like this スクリーンショット 2021-01-01 14.58.05.png

1.3 Scraping latitude and longitude from response

Get it using REXML. It is stored in the coordinates tag.

2. Get the nearest station information from latitude and longitude with Heart Rails API

2.1 Request with the latitude and longitude obtained in 1.

Click here for url

http://express.heartrails.com/api/xml?method=getStations&x=#{lon}&y=#{lat}"

The response returned is like this (multiple returns in order of closeness: eyes :) スクリーンショット 2021-01-01 14.59.22.png

2.2 Scraping the returned nearest station information

As mentioned above, the station information is included in the station tag. Specify the first station tag because it contains multiple station information.

Completed code

The code looks like this.


require 'open-uri'
require 'uri'
require "rexml/document"

## 1. request Yahoo API

### 1.1
YAHOO_APP_ID = '[Yahoo app_id]'

### 1.2
ADDRESS ='1-1 Chiyoda, Chiyoda-ku, Tokyo'
p ADDRESS
query = URI.encode_www_form(query: ADDRESS) ##encoding

request_url = "https://map.yahooapis.jp/geocode/V1/geoCoder?appid=#{YAHOO_APP_ID}&#{query}"

### 1.3 
res = OpenURI.open_uri(request_url)

doc = REXML::Document.new(res.read)
coordinates = doc.elements['YDF/Feature/Geometry/Coordinates'].text
lon,lat = coordinates.split(',')

p "longitude= #{lon} ,latitude= #{lat}"

## 2. Heart Rails API

### 2.1 1.Request with the latitude and longitude obtained in
heart_rails_request_url = "http://express.heartrails.com/api/xml?method=getStations&x=#{lon}&y=#{lat}"


### 2.2 Scrap the returned nearest station information
res = OpenURI.open_uri(heart_rails_request_url)
doc = REXML::Document.new(res.read)

station = doc.elements['response/station[1]']
name = station.elements['name'].text
line = station.elements['line'].text
distance = station.elements['distance'].text
p name
p line
p distance

Execution result

The result of requesting the nearest station at the Imperial Palace (1-1 Chiyoda, Chiyoda-ku, Tokyo).


"longitude= 139.75381447 ,latitude= 35.68382285"
"Sakuradamon"
"Tokyo Metro Yurakucho Line"
"740m"

Recommended Posts

I tried to get the distance from the address string to the nearest station with ruby
I tried upgrading from CentOS 6.5 to CentOS 7 with the upgrade tool
I tried to solve the problem of "multi-stage selection" with Ruby
I tried to get started with WebAssembly
I tried to build Ruby 3.0.0 from source
I tried to create an API to get data from a spreadsheet in Ruby (with service account)
[Ruby] I tried to diet the if statement code with the ternary operator
I tried to solve the tribonacci sequence problem in Ruby, with recursion.
[Rails] I tried to raise the Rails version from 5.0 to 5.2
I want to get the value in Ruby
I tried to get started with Spring Data JPA
[Ruby] I want to make an array from a character string with the split method. And vice versa.
I tried DI with Ruby
[Beginner's point of view] I tried to solve the FizzBuzz problem "easily" with Ruby!
I tried to increase the processing speed with spiritual engineering
[JDBC] I tried to access the SQLite3 database from Java.
What I did in the version upgrade from Ruby 2.5.2 to 2.7.1
I tried to summarize the basic grammar of Ruby briefly
I tried to automate LibreOffice Calc with Ruby + PyCall.rb (Ubuntu 18.04)
I tried to get started with Swagger using Spring Boot
I tried to interact with Java
I tried to explain the method
I tried to build the environment of PlantUML Server with Docker
[Ruby] Tonight, I tried to summarize the loop processing [times, break ...]
I tried to implement the image preview function with Rails / jQuery
I tried to reimplement Ruby Float (arg, exception: true) with builtin
I tried to check the operation of gRPC server with grpcurl
I tried to summarize the methods of Java String and StringBuilder
I tried to solve the paiza campaign problem "Challenge from Kaito 813"
[Java] How to convert from String to Path type and get the path
[Android] I want to get the listener from the button in ListView
[Ruby] I want to output only the odd-numbered characters in the character string
I tried to summarize the methods used
[Ruby] From the basics to the inject method
Follow from the root node to the lower node with JavaFX ~ I know the directions ~
I tried to implement the Iterator pattern
Get started with "Introduction to Practical Rust Programming" (Day 4) Call Rust from Ruby
I tried to summarize the Stream API
I want to get the IP address when connecting to Wi-Fi in Java
I tried to make full use of the CPU core in Ruby
I tried to visualize the access of Lambda → Athena with AWS X-Ray
[Ruby] I tried to summarize the methods that frequently appear in paiza
[Ruby] I tried to summarize the methods that frequently appear in paiza ②
I want to find out what character the character string appears from the left
I tried to implement ModanShogi with Kinx
I tried to measure and compare the speed of GraalVM with JMH
Since the du command used when the capacity is full is difficult to use, I tried wrapping it with ruby
I tried to make a program that searches for the target class from the process that is overloaded with Java
How to get the query string to actually issue when using PreparedStatement with JDBC
Ruby: I tried to find out where Nokogiri goes to see the encoding himself
What I tried when I wanted to get all the fields of a bean
I tried to solve the tribonatch sequence problem in Ruby (time limit 10 minutes)
I tried to automatically generate a class to convert from a data class to a Bundle with APT
I want to get only the time from Time type data ...! [Strftime] * Additional notes
[JDBC ③] I tried to input from the main method using placeholders and arguments.
Sample to create PDF from Excel with Ruby
I tried to verify AdoptOpenJDK 11 (11.0.2) with Docker image
I tried to make Basic authentication with Java
I tried to manage struts configuration with Coggle
I tried to manage login information with JMX
I made blackjack with Ruby (I tried using minitest)