class PlacesController

Public Instance Methods

create() click to toggle source
# File app/controllers/places_controller.rb, line 288
def create
  
  data = RDF::Reader.for(:rdfxml).new(request.request_parameters[:foo])
  sio = StringIO.new('')
  RDF::Writer.for(:json).dump( data, sio ) 
  render :json => (sio.string)
end
index() click to toggle source
# File app/controllers/places_controller.rb, line 50
def index

  @results = { 'places' => {}, 'remains'  => false}

  if params[:lon].present? and  params[:lat].present? and params[:radius].present?
    if params[:rdf_type].present?
      
      case params[:rdf_type]
      when "ug:Station", "ug_Station", "ug:station", "ug_station"

        @results_all = Station.within_spherical_circle( loc: [[ params[:lon].to_f , params[:lat].to_f ], params[:radius].to_f/6378000 ] )

      when "ug:RailWay", "ug_Railway","ug:railWay", "ug_railway"

        @results_all = Railroad.within_spherical_circle( locs: [[ params[:lon].to_f , params[:lat].to_f ], params[:radius].to_f/6378000 ] )

      when "ug:BusStop", "ug_Busstop", "ug:busStop", "ug_busstop"

        @results_all = Busstop.within_spherical_circle( loc: [[ params[:lon].to_f , params[:lat].to_f ], params[:radius].to_f/6378000 ] )

      when "ug:BusRoute", "ug_BusRoute","ug:busroute", "ug_busroute"

        @results_all = Busroute.within_spherical_circle( locs: [[ params[:lon].to_f , params[:lat].to_f ], params[:radius].to_f/6378000 ] )

      else 
        if %r^([A-Za-z]+)_([A-Za-z]+)$/ =~ params[:rdf_type]
          regexp = $1 + ':' + $2
        else
          regexp = params[:rdf_type]
        end

        @results_all = Kokosil.within_spherical_circle( loc: [[ params[:lon].to_f , params[:lat].to_f ], params[:radius].to_f/6378000 ] )
        @results_all = @results_all.where('rdf_type.value' => %r^#{regexp}$/ )

      end

    elsif params[:floor].present?

      hight = 0
      low = 0
      floor = params[:floor].scan(%r[-+\d.]+/)
      if floor.size == 2
        low  = floor[0].to_i
        high = floor[1].to_i
      else  floor.size == 1
        low  = floor[0].to_i
        high = floor[0].to_i
      end
      @results_all_k = Kokosil.criteria
      @results_all_k = @results_all_k.within_spherical_circle( loc: [[ params[:lon].to_f , params[:lat].to_f ], params[:radius].to_f/6378000 ] )
      @results_all   = @results_all_k.where( "ug_floor.value" =>  { "$gte" => low.to_s ,  "$lte" => high.to_s }  )
      @results_all  += @results_all_k.where( "ug_floor.value" =>  ""  ) if low == 0 or high == 0
      
    else # if params[:rdf_type].present?

      @results_all  = Station.within_spherical_circle( loc: [[ params[:lon].to_f , params[:lat].to_f ], params[:radius].to_f/6378000 ] )
      @results_all += Busstop.within_spherical_circle( loc: [[ params[:lon].to_f , params[:lat].to_f ], params[:radius].to_f/6378000 ] )
      @results_all += Railroad.within_spherical_circle( locs: [[ params[:lon].to_f , params[:lat].to_f ], params[:radius].to_f/6378000 ] )
      @results_all += Busroute.within_spherical_circle( locs: [[ params[:lon].to_f , params[:lat].to_f ], params[:radius].to_f/6378000 ] )
      @results_all += Kokosil.within_spherical_circle( loc: [[ params[:lon].to_f , params[:lat].to_f ], params[:radius].to_f/6378000 ] )

    end

    @results_all.each do |i|
      
      if i[:rdf_type].present?
        case  i[:rdf_type][0]['value']
        when "ug:Station"
        
          @results['places'].merge!( { "urn:ucode:_" + i.ucode =>
                                       {
                                         "http://www.w3.org/1999/02/22-rdf-syntax-ns#type" => [ { "value" => "http://uidcenter.org/vocab/ucr/" + i.rdf_type[0].value.sub(":","#") ,
                                                                                                  "type"  => "uri" } ],
                                         "http://purl.org/dc/elements/1.1/title" => i[:dc_title],
                                         "http://uidcenter.org/vocab/ucr/ug#place" => [{"value" => "https://api.opendata.ubin.jp/api/v1/places/urn:ucode:_" + i.ucode + ".geojson",
                                                                                         "type" => "literal"}],
                                         "http://www.w3.org/2003/01/geo/wgs84_pos#lat"  => i[:w3cgeo_lat],
                                         "http://www.w3.org/2003/01/geo/wgs84_pos#long" => i[:w3cgeo_long],
                                         "http://opendata.ubin.jp/puti#OperatorCode"    => i[:puti_operatorCode],
                                         "http://opendata.ubin.jp/puti#NameOfRailway"   => i[:puti_nameOfRailway],
                                         "http://opendata.ubin.jp/puti#NameOfStation"   => i[:puti_nameOfStation],
                                         "http://opendata.ubin.jp/puti#Operator"        => i[:puti_operator],
                                         "http://opendata.ubin.jp/puti#YearOfTogether"  => i[:puti_yearOfTogether],
                                         "http://opendata.ubin.jp/puti#YearOfBegin"     => i[:puti_yearOfBegin]
                                       }
                                     })
        
        when "ug:RailWay" 
      
          @results['places'].merge!( { "urn:ucode:_" + i.ucode =>
                                       {
                                         "http://www.w3.org/1999/02/22-rdf-syntax-ns#type" => [ { "value" => "http://uidcenter.org/vocab/ucr/" + i.rdf_type[0].value.sub(":","#") ,
                                                                                                  "type"  => "uri" } ],
                                         "http://purl.org/dc/elements/1.1/title" => i[:dc_title],
                                         "http://uidcenter.org/vocab/ucr/ug#place" => [{"value" => "https://api.opendata.ubin.jp/api/v1/places/urn:ucode:_" + i.ucode + ".geojson",
                                                                                         "type" => "literal"}],
                                         "http://opendata.ubin.jp/puti#OperatorCode"    => i[:puti_operatorCode],
                                         "http://opendata.ubin.jp/puti#NameOfRailway"   => i[:puti_nameOfRailway],
                                         "http://opendata.ubin.jp/puti#Operator"        => i[:puti_operator],
                                         "http://opendata.ubin.jp/puti#YearOfTogether"  => i[:puti_yearOfTogether],
                                         "http://opendata.ubin.jp/puti#YearOfBegin"     => i[:puti_yearOfBegin]
                                       }
                                     })

        when "ug:BusStop"

          @results['places'].merge!( { "urn:ucode:_" + i.ucode =>
                                       {
                                         "http://www.w3.org/1999/02/22-rdf-syntax-ns#type" => [ { "value" => "http://uidcenter.org/vocab/ucr/" + i.rdf_type[0].value.sub(":","#") ,
                                                                                                  "type"  => "uri" } ],
                                         "http://purl.org/dc/elements/1.1/title" => i[:dc_title],
                                         "http://uidcenter.org/vocab/ucr/ug#place" => [{"value" => "https://api.opendata.ubin.jp/api/v1/places/urn:ucode:_" + i.ucode + ".geojson",
                                                                                         "type" => "literal"}],
                                         "http://www.w3.org/2003/01/geo/wgs84_pos#lat"  => i[:w3cgeo_lat],
                                         "http://www.w3.org/2003/01/geo/wgs84_pos#long" => i[:w3cgeo_long],
                                         "http://opendata.ubin.jp/puti#NameOfBusStop"   => i[:puti_nameOfBusStop],
                                         "http://opendata.ubin.jp/puti#TypeOfBus"       => i[:puti_typeOfBus],
                                         "http://opendata.ubin.jp/puti#Operators"       => i[:puti_operators],
                                         "http://opendata.ubin.jp/puti#NameOfBusRoutes" => i[:puti_nameOfBusRoutes]
                                       }
                                     })
        when "ug:BusRoute"
          
          @results['places'].merge!( { "urn:ucode:_" + i.ucode =>
                                       {
                                         "http://www.w3.org/1999/02/22-rdf-syntax-ns#type" => [ { "value" => "http://uidcenter.org/vocab/ucr/" + i.rdf_type[0].value.sub(":","#") ,
                                                                                                  "type"  => "uri" } ],
                                         "http://purl.org/dc/elements/1.1/title" => i[:dc_title],
                                         "http://uidcenter.org/vocab/ucr/ug#place" => [{"value" => "https://api.opendata.ubin.jp/api/v1/places/urn:ucode:_" + i.ucode + ".geojson",
                                                                                         "type" => "literal"}],
                                         "http://opendata.ubin.jp/puti#TypeOfBus"           => i[:puti_typeOfBus],
                                         "http://opendata.ubin.jp/puti#Operator"            => i[:puti_operator],
                                         "http://opendata.ubin.jp/puti#NameOfBusRoute"      => i[:puti_nameOfBusRoute],
                                         "http://opendata.ubin.jp/puti#FrequencyInWeekday"  => i[:puti_frequencyInWeekday],
                                         "http://opendata.ubin.jp/puti#FrequencyInSaturday" => i[:puti_frequencyInSaturday],
                                         "http://opendata.ubin.jp/puti#FrequencyInHoliday"  => i[:puti_frequencyInHoliday]
                                       }
                                     })


        else

          @results['places'].merge!( { "urn:ucode:_" + i.ucode =>
                                        {
                                          "http://uidcenter.org/vocab/ucr/ug#place" => [{"value" => "https://api.opendata.ubin.jp/api/v1/places/urn:ucode:_" + i.ucode + ".geojson",
                                                                                          "type" => "literal"}],
                                          "http://www.w3.org/2003/01/geo/wgs84_pos#lat"  => i[:w3cgeo_lat],
                                          "http://www.w3.org/2003/01/geo/wgs84_pos#long" => i[:w3cgeo_long],
                                        }
                                      })

          @results['places']["urn:ucode:_" + i.ucode].merge!({
                                                                "http://purl.org/dc/elements/1.1/title"   => i[:dc_title],
                                                              }) if i[:dc_title].present?
          @results['places']["urn:ucode:_" + i.ucode].merge!({
                                                                "http://purl.org/dc/elements/1.1/time"   => i[:dc_time],
                                                              }) if i[:dc_time].present?
          @results['places']["urn:ucode:_" + i.ucode].merge!({
                                                                "http://uidcenter.org/vocab/ucr/ug#type"  => i[:ug_type],
                                                              }) if i[:ug_type].present?
          @results['places']["urn:ucode:_" + i.ucode].merge!({
                                                                "http://uidcenter.org/vocab/ucr/ug#floor" => i[:ug_floor],
                                                              }) if i[:ug_floor].present?
          @results['places']["urn:ucode:_" + i.ucode].merge!({
                                                                "http://uidcenter.org/vocab/ucr/uc#temperature" => i[:uc_temperature]
                                                              }) if i[:uc_temperature].present?
          @results['places']["urn:ucode:_" + i.ucode].merge!({
                                                              "http://uidcenter.org/vocab/ucr/uc#pollen" => i[:uc_pollen]
                                                              }) if i[:uc_pollen].present?
          @results['places']["urn:ucode:_" + i.ucode].merge!({
                                                                "http://uidcenter.org/vocab/ucr/uc#humidity" => i[:uc_humidity]
                                                              }) if i[:uc_humidity].present?
          @results['places']["urn:ucode:_" + i.ucode].merge!({
                                                                "http://uidcenter.org/vocab/ucr/uc#address" => i[:ug_address]
                                                              }) if i[:ug_address].present?
          if i[:rdf_type].present?

            if %r^ug:(\w+)/ =~ i['rdf_type'][0]['value'].to_s
              uri = "http://uidcenter.org/vocab/ucr/ug#" + $1
            elsif %r^puti:(\w+)/ =~ i['rdf_type'][0]['value'].to_s
              uri = "http://opendata.ubin.jp/puti#" + $1
            end

            @results['places']["urn:ucode:_" + i.ucode].merge!({
                                                                 "http://www.w3.org/1999/02/22-rdf-syntax-ns#type" => [
                                                                                                                       { "value" => uri,
                                                                                                                         "type"  => "uri"
                                                                                                                       }
                                                                                                                      ]
                                                               }) unless uri.nil?
          end # i[:rdf_type].present 

          
        end # case  i.rdf_type[0].value
      else
        @results['places'].merge!( { "urn:ucode:_" + i.ucode => {
                                       "http://purl.org/dc/elements/1.1/title"   => i[:dc_title],
                                       "http://uidcenter.org/vocab/ucr/ug#type"  => i[:ug_type],
                                       "http://uidcenter.org/vocab/ucr/ug#floor" => i[:ug_floor],
                                       "http://uidcenter.org/vocab/ucr/ug#place" => [{"value" => "https://api.opendata.ubin.jp/api/v1/places/urn:ucode:_" + i.ucode + ".geojson",
                                                                                       "type" => "literal"}],
                                       "http://www.w3.org/2003/01/geo/wgs84_pos#lat"  => i[:w3cgeo_lat],
                                       "http://www.w3.org/2003/01/geo/wgs84_pos#long" => i[:w3cgeo_long],
                                       "http://uidcenter.org/vocab/ucr/ug#address"    => i[:ug_address]
                                     }
                                   })
        
      end  # if i[:rdf_type].present?
    end # @results_all.each do |i|

    if @results_all.nil? or @results_all.length == 0
      head :not_found and return
    end


    str = JSON.generate(@results['places'])
    str2 =  JSON.utf8_to_json_ascii(str)
    str2.gsub!(%r\\"/,'"')
    str2.gsub!(%r\\u/,'u')
    str2.gsub!(%ru([0-9a-f][0-9a-f][0-9a-f][0-9a-f])/,'\U\1')

    data = RDF::Reader.for(:json).new(str2)

    sio = StringIO.new('')

    RDF::Writer.for(:rdfxml).dump( data, sio, :standard_prefixes=>true )

    respond_to do |format|
      format.json      {  render :json => JSON.pretty_generate(@results) } 
      format.xml       {  render :xml  => sio.string.gsub(%rU([0-9a-z]{4})/) {|s| [$1.to_i(16)].pack("U")} }
    end
    
  else
    head :bad_request
  end
end
show() click to toggle source
# File app/controllers/places_controller.rb, line 6
def show

  @results = Hash.new

  if params[:id].present?
    ucode = params[:id].sub("urn:ucode:_","").sub("ucode_","")
      if (@results_all  = Station.where( "ucode" =>  ucode ) and @results_all.length == 1 ) then

        @results['type'] = "Point"
        @results['coordinates'] = @results_all[0]['loc']
        
      elsif ( @results_all = Busstop.where( "ucode" =>  ucode ) and  @results_all.length == 1 ) then
   
        @results['type'] = "Point"
        @results['coordinates'] = @results_all[0]['loc']

      elsif ( @results_all = Railroad.where( "ucode" =>  ucode ) and @results_all.length == 1 ) then
   
        @results = @results_all[0]['ug_place']

      elsif ( @results_all = Busroute.where( "ucode" =>  ucode ) and @results_all.length == 1 ) then
      
        @results = @results_all[0]['ug_place']

      elsif ( @results_all = Kokosil.where( "ucode" =>  %r#{ucode}/ ) and @results_all.length == 1 ) then
      
        @results['type'] = "Point"
        @results['coordinates'] = @results_all[0]['loc']

      else
        head :not_found and return
      end
      if @results_all.length == 1 then

        respond_to do |format|
            format.json      {  render :json => JSON.pretty_generate(@results) } 
            format.geojson   {  render :json => JSON.pretty_generate(@results) }
        end
      end
  else
       head :bad_request
  end
end