<commit_before>require 'utils'

##
# Implements OWL API operations.
# See: http://wiki.openstreetmap.org/wiki/OWL_(OpenStreetMap_Watch_List)/API
#
class MapApiController < ApplicationController
  include ApiHelper

  def kothic
    @x, @y, @zoom = get_xyz(params)
    tile_bbox = tile2bbox(@x, @y, @zoom)
    p tile_bbox
    tiles = ActiveRecord::Base.connection.select_all("
      WITH tile_bbox AS (
        SELECT ST_Transform(ST_SetSRID('LINESTRING(#{tile_bbox[0]} #{tile_bbox[1]},
          #{tile_bbox[2]} #{tile_bbox[3]})'::geometry, 4326), 900913)::box2d g
      )
      (SELECT DISTINCT ON (w.id, t.x, t.y) ST_AsGeoJSON(ST_TransScale(ST_Transform(geom, 900913),
        (SELECT -ST_XMin(g) FROM tile_bbox),
        (SELECT -ST_YMin(g) FROM tile_bbox),
        (SELECT 10000 / (ST_XMax(g) - ST_XMin(g)) FROM tile_bbox),
        (SELECT 10000 / (ST_YMax(g) - ST_YMin(g)) FROM tile_bbox)), 0) AS geojson,
        w.tags
      FROM way_tiles t
      INNER JOIN ways w ON (w.id = t.way_id AND w.version = t.version)
      WHERE x >= #{@x * (2 ** (16 - @zoom))} AND y >= #{@y * (2 ** (16 - @zoom))} AND
        x <= #{(@x + 1) * (2 ** (16 - @zoom))} AND y <= #{(@y + 1) * (2 ** (16 - @zoom))}
      ORDER BY w.id, t.x, t.y, t.rev DESC)
        UNION
      (SELECT DISTINCT ON (n.id) ST_AsGeoJSON(ST_TransScale(ST_Transform(n.geom, 900913),
        (SELECT -ST_XMin(g) FROM tile_bbox),
        (SELECT -ST_YMin(g) FROM tile_bbox),
        (SELECT 10000 / (ST_XMax(g) - ST_XMin(g)) FROM tile_bbox),
        (SELECT 10000 / (ST_YMax(g) - ST_YMin(g)) FROM tile_bbox)), 0) AS geojson,
        n.tags
      FROM nodes n
      WHERE geom && (SELECT ST_Transform(ST_Setsrid(g, 900913), 4326) FROM tile_bbox)
        AND n.tags != ''::hstore
      ORDER BY n.id, n.version DESC)
      LIMIT 10000
      ").to_a

    geojson = []

    for tile in tiles
      kothic_tile = JSON[tile['geojson']]
      kothic_tile['properties'] = eval("{#{tile['tags']}}")
      geojson << kothic_tile
    end
    text = "onKothicDataResponse({\"features\": #{JSON[geojson].as_json}, \"bbox\":#{tile_bbox}, \"granularity\": 10000 },
      #{@zoom}, #{@x}, #{@y})"
    render :js => text
  end

end<commit_msg>More improvements on the Kothic API.<commit_after>require 'utils'

##
# Implements OWL API operations.
# See: http://wiki.openstreetmap.org/wiki/OWL_(OpenStreetMap_Watch_List)/API
#
class MapApiController < ApplicationController
  include ApiHelper

  def kothic
    @x, @y, @zoom = get_xyz(params)
    tile_bbox = tile2bbox(@x, @y, @zoom)
    p tile_bbox
    tiles = ActiveRecord::Base.connection.select_all("
      WITH tile_bbox AS (
        SELECT ST_Transform(ST_SetSRID('LINESTRING(#{tile_bbox[0]} #{tile_bbox[1]},
          #{tile_bbox[2]} #{tile_bbox[3]})'::geometry, 4326), 900913)::box2d g
      )
      (SELECT DISTINCT ON (w.id, t.x, t.y) ST_AsGeoJSON(ST_TransScale(ST_Transform(
        CASE WHEN geometrytype(st_makevalid(geom)) = 'LINESTRING' AND st_isclosed(geom) and st_issimple(geom) and st_isvalid(geom) THEN st_forcerhr(st_makepolygon(geom)) ELSE geom END, 900913),
        (SELECT -ST_XMin(g) FROM tile_bbox),
        (SELECT -ST_YMin(g) FROM tile_bbox),
        (SELECT 10000 / (ST_XMax(g) - ST_XMin(g)) FROM tile_bbox),
        (SELECT 10000 / (ST_YMax(g) - ST_YMin(g)) FROM tile_bbox)), 0) AS geojson,
        w.tags
      FROM way_tiles t
      INNER JOIN ways w ON (w.id = t.way_id AND w.version = t.version)
      WHERE x >= #{@x * (2 ** (16 - @zoom))} AND y >= #{@y * (2 ** (16 - @zoom))} AND
        x <= #{(@x + 1) * (2 ** (16 - @zoom))} AND y <= #{(@y + 1) * (2 ** (16 - @zoom))}
      ORDER BY w.id, t.x, t.y, t.rev DESC)
        UNION
      (SELECT DISTINCT ON (n.id) ST_AsGeoJSON(ST_TransScale(ST_Transform(n.geom, 900913),
        (SELECT -ST_XMin(g) FROM tile_bbox),
        (SELECT -ST_YMin(g) FROM tile_bbox),
        (SELECT 10000 / (ST_XMax(g) - ST_XMin(g)) FROM tile_bbox),
        (SELECT 10000 / (ST_YMax(g) - ST_YMin(g)) FROM tile_bbox)), 0) AS geojson,
        n.tags
      FROM (SELECT DISTINCT ON (id) id, visible, geom, tags, version
            FROM nodes
            WHERE geom && (SELECT ST_Transform(ST_Setsrid(g, 900913), 4326) FROM tile_bbox) AND tags != ''::hstore
            ORDER BY id, version DESC) n
      WHERE n.visible
      ORDER BY n.id, n.version DESC)
      --LIMIT 10000
      ").to_a

    geojson = []

    for tile in tiles
      kothic_tile = JSON[tile['geojson']]
      kothic_tile['properties'] = eval("{#{tile['tags']}}")
      geojson << kothic_tile
    end
    text = "onKothicDataResponse({\"features\": #{JSON[geojson].as_json}, \"bbox\":#{tile_bbox}, \"granularity\": 10000 },
      #{@zoom}, #{@x}, #{@y})"
    render :js => text
  end

end
{'fraction_non_alphanumeric': 0.1638682252922423, 'fraction_numerical': 0.035706695005313496, 'mean_word_length': 2.8956953642384105, 'pattern_counts': {'":': 6, '<': 13, '<?xml version=': 0, '>': 9, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '4031894', 'n_tokens_mistral': 1959, 'n_tokens_neox': 1807, 'n_words': 426}