Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
…down.js

Conflicts:
	geowebservice/geodatabase/services.py
  • Loading branch information
Your Name committed Jun 13, 2019
2 parents d9f400d + 844ed0a commit 8358e4b
Show file tree
Hide file tree
Showing 13 changed files with 92 additions and 22 deletions.
2 changes: 1 addition & 1 deletion demo.html
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,6 @@
</footer>
</body>

<html>
</html>


Empty file.
Empty file.
35 changes: 35 additions & 0 deletions geowebservice/geodatabase/management/commands/load_data.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
# -*- coding: utf-8 -*-
from __future__ import print_function
import pysolr

from django.core.management.base import BaseCommand, CommandError

import ast
from django.conf import settings

from geodatabase.services import ServiceSolr

import logging
logger = logging.getLogger()
logging.basicConfig(level=logging.DEBUG)

class Command(BaseCommand):
help = 'Closes the specified poll for voting'

def add_arguments(self, parser):
parser.add_argument('all', nargs='+', type=str)
parser.add_argument('countries', nargs='+', type=str)

def handle(self, *args, **options):
all_locations = ''
countries_names = ''
try:
all_locations = options['all'][0]
countries_names = options['countries'][0]
except:
all_locations = '/home/leonardo/Área de Trabalho/geonames_files/allCountries.txt'
countries_names = '/home/leonardo/Área de Trabalho/geonames_files/country.csv'

s = ServiceSolr()
s.load_contry_info(countries_names)
s.load_initial_data(all_locations)
15 changes: 8 additions & 7 deletions geowebservice/geodatabase/services.py
Original file line number Diff line number Diff line change
Expand Up @@ -66,11 +66,11 @@ def __init__(self, host="solr", port="8983", path="/solr", timeout=CONNECTION_TI
try:
self.__fetch_initial_settings()
except:
self.SOLR_HOST = host
self.SOLR_PORT = port
self.SOLR_PATH = path
self.solr = pysolr.Solr('http://' +self.SOLR_HOST+ ':'+ self.SOLR_PORT+self.SOLR_PATH+'/'+core, timeout=timeout)
self.SOLR_HOST = "localhost"
self.SOLR_PORT = "8983"
self.SOLR_PATH = "/solr"

self.solr = pysolr.Solr('http://' +self.SOLR_HOST+ ':'+ self.SOLR_PORT+self.SOLR_PATH+'/', timeout=timeout)
logger.info("Connected to Solr")

def load_contry_info(self, countryFile="country.csv"):
Expand All @@ -97,12 +97,13 @@ def load_initial_data(self, allCountriesFile):
spamreader = UnicodeReader(csvfile, delimiter='\t', quotechar='|')
list_docs_to_commit = []
i = 0

for row in spamreader:
if not (row[1]=='Earth' or row[7] == 'CONT' or row[7] == 'PCLI' or row[7] == 'ISLS' or row[7] == 'ADM1' or row[7] == 'ADM2' or row[7] == 'ADM3' or row[7] == 'ADM4'):
continue
i = i + 1
d = {}

d['id'] = i
d['geonameId_t'] = row[0]
print(row[0])
d['name_t'] = row[1]
Expand All @@ -129,7 +130,7 @@ def load_initial_data(self, allCountriesFile):
if row[0] in self.contryInfo:
d['continent_t'] = self.contryInfo[row[0]]['continent']
d['name_t'] = self.contryInfo[row[0]]['name']

#print(d)
d = dict(d.items())
list_docs_to_commit.append(d)
Expand Down
47 changes: 38 additions & 9 deletions geowebservice/geodatabase/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -50,31 +50,31 @@ def detail(request, geonameid):
d = results.docs[0]

# Fetch the data to variables, only to facilitate the access and to become the code easy to read.
fcode = d['fcode_t'][0]
name = d['name_t'][0]
fcode = d['fcode_t']
name = d['name_t']

try:
country = d['country_t'][0]
country = d['country_t']
except:
pass

try:
admin1 = d['admin1_t'][0]
admin1 = d['admin1_t']
except:
pass

try:
admin2 = d['admin2_t'][0]
admin2 = d['admin2_t']
except:
pass

try:
admin3 = d['admin3_t'][0]
admin3 = d['admin3_t']
except:
pass

try:
admin4 = d['admin4_t'][0]
admin4 = d['admin4_t']
except:
pass
else:
Expand Down Expand Up @@ -156,7 +156,7 @@ def buildJson(response_object,response_data):
def addEntry(geoname,response_data):
response = {}
print(geoname)
response['geonameid'] = int(geoname['geonameId_t'][0])
response['geonameid'] = int(geoname['geonameId_t'])
response['name'] = geoname['name_t']
response['fcode'] = geoname['fcode_t']
try:
Expand All @@ -168,4 +168,33 @@ def addEntry(geoname,response_data):
except:
pass

return [response]
return [response]

"""
Get coordinates by location name and fcode
"""
def getCoordinates(request,location):
if location == '' or location == None:
return HttpResponse(json.dumps([]),content_type="application/json")

location = location.strip().split(',')
levels = [ 'PCLI', 'ADM1', 'ADM2', 'ADM3', 'ADM4', 'ADM5' ]
fcode = levels[len(location)-1]
name = location[0]

try:
solr = ServiceSolr()
response_object = solr.search("fcode_t:"+fcode+" AND name_t:"+name)
if fcode == 'ADM1' and response_object.hits == 0:
response_object = solr.search("fcode_t:ISLS AND name_t:"+name)
response_object = response_object.docs[0]
response_data = buildCoordinates(response_object,[])
return HttpResponse(json.dumps(response_data),content_type="application/json")
except:
return HttpResponse(json.dumps([]),content_type="application/json")

"""
Build a list with latitude and longitude of the location
"""
def buildCoordinates(response_object,response_data):
return [ response_object['latitude_f'], response_object['longitude_f'] ]
2 changes: 1 addition & 1 deletion geowebservice/geowebservice/settings.py
Original file line number Diff line number Diff line change
Expand Up @@ -174,7 +174,7 @@
}
}

SOLR_HOST = "solr"
SOLR_HOST = "localhost"
SOLR_PORT = "8983"
SOLR_PATH = "/solr"

Expand Down
5 changes: 5 additions & 0 deletions geowebservice/geowebservice/urls.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,13 @@
# url(r'^$', '{{ project_name }}.views.home', name='home'),
# url(r'^{{ project_name }}/', include('{{ project_name }}.foo.urls')),
# url(r'^geodatabase/$', 'geodatabase.views.index'),

# get child locations by geonameid
url(r'^geodatabase/(?P<geonameid>\d+)/$', 'geodatabase.views.detail'),

# get coordinates by name and fcode
url(r'^geodatabase/(?P<location>[a-zA-Z, ]+)/$', 'geodatabase.views.getCoordinates'),

# Uncomment the admin/doc line below to enable admin documentation:
# url(r'^admin/doc/', include('django.contrib.admindocs.urls')),

Expand Down
Binary file added geowebservice/sqlite3.db
Binary file not shown.
2 changes: 1 addition & 1 deletion js/adm1.js
Original file line number Diff line number Diff line change
Expand Up @@ -94,7 +94,7 @@ function populateADM1(self,instanceLocal){
}
else{
for(i=0;i<self.levels[self.level-1].length;i++){
if(self.levels[self.level-1][i]['name'] == self.selectedADM1Text)
if(self.levels[self.level-1][i]['name'] == stripGCode(self.selectedADM1Text))
self.geoClick(undefined,instanceLocal,self.levels[self.level-1][i]['geonameId']);
}
}
Expand Down
2 changes: 1 addition & 1 deletion js/adm2.js
Original file line number Diff line number Diff line change
Expand Up @@ -95,7 +95,7 @@ function populateADM2(self,instanceLocal){
}
else{
for(i=0;i<self.levels[self.level-1].length;i++){
if(self.levels[self.level-1][i]['name'] == self.selectedADM2Text)
if(self.levels[self.level-1][i]['name'] == stripGCode(self.selectedADM2Text))
self.geoClick(undefined,instanceLocal,self.levels[self.level-1][i]['geonameId']);
}
}
Expand Down
2 changes: 1 addition & 1 deletion js/adm3.js
Original file line number Diff line number Diff line change
Expand Up @@ -95,7 +95,7 @@ function populateADM3(self,instanceLocal){
}
else{
for(i=0;i<self.levels[self.level-1].length;i++){
if(self.levels[self.level-1][i]['name'] == self.selectedADM3Text)
if(self.levels[self.level-1][i]['name'] == stripGCode(self.selectedADM3Text))
self.geoClick(undefined,instanceLocal,self.levels[self.level-1][i]['geonameId']);
}
}
Expand Down
2 changes: 1 addition & 1 deletion js/adm4.js
Original file line number Diff line number Diff line change
Expand Up @@ -93,7 +93,7 @@ function populateADM4(self,instanceLocal){
}
else{
for(i=0;i<self.levels[self.level-1].length;i++){
if(self.levels[self.level-1][i]['name'] == self.selectedADM4Text)
if(self.levels[self.level-1][i]['name'] == stripGCode(self.selectedADM4Text))
self.geoClick(undefined,instanceLocal,self.levels[self.level-1][i]['geonameId']);
}
}
Expand Down

0 comments on commit 8358e4b

Please sign in to comment.