bart_lines.py 2.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364
  1. # Licensed to the Apache Software Foundation (ASF) under one
  2. # or more contributor license agreements. See the NOTICE file
  3. # distributed with this work for additional information
  4. # regarding copyright ownership. The ASF licenses this file
  5. # to you under the Apache License, Version 2.0 (the
  6. # "License"); you may not use this file except in compliance
  7. # with the License. You may obtain a copy of the License at
  8. #
  9. # http://www.apache.org/licenses/LICENSE-2.0
  10. #
  11. # Unless required by applicable law or agreed to in writing,
  12. # software distributed under the License is distributed on an
  13. # "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
  14. # KIND, either express or implied. See the License for the
  15. # specific language governing permissions and limitations
  16. # under the License.
  17. import json
  18. import pandas as pd
  19. import polyline
  20. from sqlalchemy import String, Text
  21. from superset import db
  22. from superset.utils.core import get_example_database
  23. from .helpers import get_example_data, TBL
  24. def load_bart_lines(only_metadata=False, force=False):
  25. tbl_name = "bart_lines"
  26. database = get_example_database()
  27. table_exists = database.has_table_by_name(tbl_name)
  28. if not only_metadata and (not table_exists or force):
  29. content = get_example_data("bart-lines.json.gz")
  30. df = pd.read_json(content, encoding="latin-1")
  31. df["path_json"] = df.path.map(json.dumps)
  32. df["polyline"] = df.path.map(polyline.encode)
  33. del df["path"]
  34. df.to_sql(
  35. tbl_name,
  36. database.get_sqla_engine(),
  37. if_exists="replace",
  38. chunksize=500,
  39. dtype={
  40. "color": String(255),
  41. "name": String(255),
  42. "polyline": Text,
  43. "path_json": Text,
  44. },
  45. index=False,
  46. )
  47. print("Creating table {} reference".format(tbl_name))
  48. tbl = db.session.query(TBL).filter_by(table_name=tbl_name).first()
  49. if not tbl:
  50. tbl = TBL(table_name=tbl_name)
  51. tbl.description = "BART lines"
  52. tbl.database = database
  53. db.session.merge(tbl)
  54. db.session.commit()
  55. tbl.fetch_metadata()