\r\n

51Degrees Location Python  4.3

Reverse Geo-Location services for 51Degrees Pipeline

cloud/gettingstarted.py

To run this example, you will need to create a resource key. The resource key is used as shorthand to store the particular set of properties you are interested in as well as any associated license keys that entitle you to increased request limits and/or paid-for properties.

You can create a resource key using the 51Degrees Configurator.

Expected output:

1 What country is at coordinates:-0.9822207999999999, 51.458048?
2 United Kingdom
1 # *********************************************************************
2 # This Original Work is copyright of 51 Degrees Mobile Experts Limited.
3 # Copyright 2019 51 Degrees Mobile Experts Limited, 5 Charlotte Close,
4 # Caversham, Reading, Berkshire, United Kingdom RG4 7BY.
5 #
6 # This Original Work is licensed under the European Union Public Licence (EUPL)
7 # v.1.2 and is subject to its terms as set out below.
8 #
9 # If a copy of the EUPL was not distributed with this file, You can obtain
10 # one at https://opensource.org/licenses/EUPL-1.2.
11 #
12 # The 'Compatible Licences' set out in the Appendix to the EUPL (as may be
13 # amended by the European Commission) shall be deemed incompatible for
14 # the purposes of the Work and the provisions of the compatibility
15 # clause in Article 5 of the EUPL shall not apply.
16 #
17 # If using the Work as, or as part of, a network application, by
18 # including the attribution notice(s) required under Article 5 of the EUPL
19 # in the end user terms of the application under an appropriate heading,
20 # such notice(s) shall fulfill the requirements of that article.
21 # ********************************************************************
22 
23 
24 
34 
35 from fiftyone_location.location_pipelinebuilder import LocationPipelineBuilder
36 
37 # First create the device detection pipeline with the desired settings.
38 
39 # You need to create a resource key at https://configure.51degrees.com
40 # and paste it into the code, replacing !!YOUR_RESOURCE_KEY!! below.
41 # Alternatively, add a resource_key environment variable
42 import os
43 if "resource_key" in os.environ:
44  resource_key = os.environ["resource_key"]
45 else:
46  resource_key = "!!YOUR_RESOURCE_KEY!!"
47 
48 if resource_key == "!!YOUR_RESOURCE_KEY!!":
49  print("""
50  You need to create a resource key at
51  https://configure.51degrees.com and paste it into the code,
52  'replacing !!YOUR_RESOURCE_KEY!!
53  To get a resourcekey with the properties used in this example go to https://configure.51degrees.com/GCrtGh1L
54  """)
55 else:
56 
57  pipeline = LocationPipelineBuilder(resource_key=resource_key).build()
58 
59  # We create a FlowData object from the pipeline
60  # this is used to add evidence to and then process
61 
62  flow_data = pipeline.create_flowdata()
63 
64  # Here we add a longitude and latitude as evidence
65 
66  latitude = "51.458048"
67  longitude = "-0.9822207999999999"
68 
69  flow_data.evidence.add("query.51D_Pos_latitude", latitude)
70  flow_data.evidence.add("query.51D_Pos_longitude", longitude)
71 
72  # Now we process the FlowData
73 
74  flow_data.process()
75 
76  print("What country is at coordinates:" + longitude + ", " + latitude + "?")
77  if flow_data.location.country.has_value():
78  print(flow_data.location.country.value())
79  else:
80  print(flow_data.location.no_value_message())