ruby 上海街道数据导入
Posted
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了ruby 上海街道数据导入相关的知识,希望对你有一定的参考价值。
task :shjd => :environment do
layer = RegionLayer.find(185562)
region_style_id = layer.default_region_style.id
layer.regions.map(&:soft_del)
layer.attr_fields.delete_all
id_field_id = AttrField.create!(layer: layer, field: 'QH_NAME', is_title: true)&.id
name_field_id = AttrField.create!(layer: layer, field: 'QH_CODE')&.id
geojson_path="#{Rails.root}/shanghai.json"
features=JSON.parse(File.read(geojson_path))
features.each do |f|
attrs = {
id_field_id.to_s => f['QH_NAME'],
name_field_id.to_s => f['QH_CODE']
}
p attrs
type = f['json_geometry']['type']
path = []
if type == 'Polygon'
coord_points=f['json_geometry']['coordinates']
coord_points.each_with_index do |cps, index|
part = []
cps.each do |point|
lng = point[0]
lat = point[1]
lng, lat = DtwyGeocoder.wgs_to_bd(lng, lat)
part << "#{lng},#{lat}"
end
path << part.join(';')
end
else
coord_points=f['json_geometry']['coordinates']
coord_points.each_with_index do |parts, index|
parts.each do |cps|
part = []
cps.each do |point|
lng = point[0]
lat = point[1]
lng, lat = DtwyGeocoder.wgs_to_bd(lng, lat)
part << "#{lng},#{lat}"
end
path << part.join(';')
end
end
end
path = path.join('|')
encoded_path = Geospatial.path_encode(path)
layer.regions.create!({
map_id: layer.map_id,
user_id: layer.user_id,
region_type: 0,
path: path,
encoded_path: encoded_path,
region_style_id: region_style_id,
attrs: attrs
})
end
end
以上是关于ruby 上海街道数据导入的主要内容,如果未能解决你的问题,请参考以下文章
上海居住证
上海常用电话及地址
ruby 导入数据临时代码
使用 Ruby/Chef Recipe for Vagrant 导入 Mysql 数据库
Ruby on Rails - 从 CSV 文件导入数据
Ruby:使用ActiveRecord验证将CSV数据导入DB