comparison RestDbGisApi.py @ 278:4ade9b80b563 default tip

more cleanup. descriptions work better now.
author casties
date Fri, 24 Feb 2012 16:41:30 +0100
parents 55bc9972fb1b
children
comparison
equal deleted inserted replaced
277:9bfa7a6858f1 278:4ade9b80b563
479 kmlPlace['TimeSpan1'] = '%s'%end_yr 479 kmlPlace['TimeSpan1'] = '%s'%end_yr
480 else: 480 else:
481 kmlPlace['TimeStamp'] = '%s'%beg_yr 481 kmlPlace['TimeStamp'] = '%s'%beg_yr
482 482
483 if geomField=='point': 483 if geomField=='point':
484 kmlPlace['description'] = "<![CDATA[%s]]>"%desc 484 #kmlPlace['description'] = "<![CDATA[%s]]>"%desc
485 kmlPlace['description'] = desc
485 486
486 kmlPlace['icon'] = '#marker_icon_'+colorField 487 kmlPlace['icon'] = '#marker_icon_'+colorField
487 kmlPlace['coord_x'] = str(xCoord) 488 kmlPlace['coord_x'] = str(xCoord)
488 kmlPlace['coord_y'] = str(yCoord) 489 kmlPlace['coord_y'] = str(yCoord)
489 kmlPlace['coord_z'] = '50' 490 kmlPlace['coord_z'] = '50'
601 602
602 return line_coords 603 return line_coords
603 604
604 ## legacy methods... 605 ## legacy methods...
605 606
606 def getKmlUrl(self,schema='chgis',table='mpdl',args={'doc':None,'id':None}):
607 logging.debug("getKmlUrl")
608 id=args.get('id')
609 doc=args.get('doc')
610 data = self.getDataForGoogleMap(schema,table,id,doc)
611 kml=self.getKMLname(data=data,table=table)
612 baseUrl = self.absolute_url()
613 return "%s/daten/%s"%(baseUrl,kml)
614
615 def getDataForGoogleMap(self,schema='chgis',table='mpdl',id=None,doc=None):
616 logging.debug("getDataForGoogleMap")
617 qstr="SELECT * FROM "+schema+"."+table
618 try:
619 if id is not None:
620 qstr=qstr+" WHERE "
621 for id_item in id.split(","):
622 if table=='mpdl':
623 qstr=qstr+" mpdl_xmlsource_id = '"+id_item+ "' OR"
624 else:
625 qstr=qstr+" cast(id as text) LIKE '"+id_item+ "' OR"
626 qstr=str(qstr).rsplit(" ",1)[0] #to remove last " and "
627 data=self.ZSQLSimpleSearch(qstr)
628 return data
629 except:
630 return qstr
631
632
633 def getKMLname(self,data=[],table=""):
634 logging.debug("getKMLname")
635 #session=context.REQUEST.SESSION
636 kml4Marker="<kml xmlns=\'http://www.opengis.net/kml/2.2\'><Document>"
637 kml4Marker+="<Style id=\'marker_icon_red\'><IconStyle><scale>15</scale><Icon><href>http://mappit.mpiwg-berlin.mpg.de/mappit/icons/dot_red.png</href></Icon></IconStyle></Style>\n"
638 kml4Marker+="<Style id=\'marker_icon_black\'><IconStyle><scale>15</scale><Icon><href>http://mappit.mpiwg-berlin.mpg.de/mappit/icons/dot_black.png</href></Icon></IconStyle></Style>\n"
639 kml4Marker+="<Style id=\'marker_icon_blue\'><IconStyle><scale>15</scale><Icon><href>http://mappit.mpiwg-berlin.mpg.de/mappit/icons/dot_blue.png</href></Icon></IconStyle></Style>\n"
640 kml4Marker+="<Style id=\'marker_icon_green\'><IconStyle><scale>15</scale><Icon><href>http://mappit.mpiwg-berlin.mpg.de/mappit/icons/dot_green.png</href></Icon></IconStyle></Style>\n"
641 kml4Marker+="<Style id=\'marker_icon_violett\'><IconStyle><scale>15</scale><Icon><href>http://mappit.mpiwg-berlin.mpg.de/mappit/icons/dot_violett.png</href></Icon></IconStyle></Style>\n"
642 initializeStringForGoogleMaps=""
643 #doLine=container.getVar('doLine')
644 # Mapping a set of points from table-based SQL-query:
645 if data!=None:
646 try:
647 SQL='SELECT "attribute with gis_id" FROM public.metadata WHERE tablename = %s'
648 res = self.executeSQL(SQL, (table,))
649 gisIDattribute = res['rows'][0][0]
650 except:
651 return "table not registered within metadata"
652
653 for dataset in data:
654 try:
655 xCoord=getattr(dataset,'longitude')
656 yCoord=getattr(dataset,'latitude')
657 except:
658 try:
659 xCoord=getattr(dataset,'x_coord')
660 yCoord=getattr(dataset,'y_coord')
661 except:
662 #try:
663 gisID=getattr(dataset,gisIDattribute)
664 coords=self.getPoint4GISid(gisID)
665 if coords!=None:
666 xCoord=coords[0]
667 yCoord=coords[1]
668 # except:
669 # return "no coordinates found"
670
671 if float(xCoord)!=0:
672 if float(yCoord)!=0:
673 kml4Marker=kml4Marker+"<Placemark>"
674 kml4Marker=kml4Marker+"<description> <![CDATA[<b>"
675 for values in dataset:
676 #logging.debug("values=%s"%repr(values))
677 if values != (None, None):
678 if str(values).find('name')>-1:
679 kml4Marker=kml4Marker+"<name>"+str(values[1])+"</name>\n"
680 continue
681 elif str(values).find('place')>-1:
682 kml4Marker=kml4Marker+"<name>"+str(values[1])+"</name>\n"
683 continue
684
685 kml4Marker=kml4Marker+str(values)+": "
686 attribute_string=str(values).replace("'","__Apostroph__")
687 attribute_string=str(attribute_string).replace('"','__DoubleApostroph__')
688 attribute_string=str(attribute_string).replace(';','__$$__')
689 attribute_string=str(attribute_string).replace('&','&amp;')
690 if str(attribute_string).find('http')>-1:
691 attribute_string='<A HREF=' + str(attribute_string) + ' target="_blank">' + str(attribute_string) + '</A>'
692 kml4Marker=kml4Marker+attribute_string+"</a><br>\n"
693
694 kml4Marker=kml4Marker+"]]></description>\n"
695 kml4Marker=kml4Marker+"<styleURL>#marker_icon_red</styleURL>\n"
696 kml4Marker=kml4Marker+"<Point>"
697
698 kml4Marker=kml4Marker+"<coordinates>"+str(xCoord)+","+str(yCoord)+",0</coordinates>\n"
699 kml4Marker=kml4Marker+"</Point>\n"
700 kml4Marker=kml4Marker+"</Placemark>\n"
701
702 kml4Marker=kml4Marker+"</Document>\n</kml>"
703 kmlFileName="marker"+str(time.time())+".kml"
704
705 #kml4Marker=str(kml4Marker).replace('&','$$')
706 #kml4Marker=str(kml4Marker).replace(';','__$$__')
707 #kml4Marker=str(kml4Marker).replace('#','__SHARP__')
708 isLoadReady='false'
709 while isLoadReady=='false':
710 isLoadReady=self.RESTwrite2File(self.daten,kmlFileName,kml4Marker)
711
712 return kmlFileName
713
714 def trydatahas_key(self,data,index,key_string): 607 def trydatahas_key(self,data,index,key_string):
715 logging.debug("trying:"+key_string) 608 logging.debug("trying:"+key_string)
716 try: 609 try:
717 return data[index].has_key(key_string) 610 return data[index].has_key(key_string)
718 except: 611 except:
719 return 'false' 612 return 'false'
720 613
721 # def getGoogleMapString(self,kml):
722 # logging.debug("getGoogleMapString")
723 # printed= '<body %s> '%kml +"""\n <div id="map_canvas" style="width: 98%; height: 95%"> </div> \n </body>" \n </html>"""
724 # return printed
725
726 def getPoint4GISid(self,gis_id):
727 j=0
728 coords=(0,0)
729 if gis_id != None:
730 while (True):
731 j=j+1
732 if (j>100): # FJK: just to prevent endless loops
733 break
734 if (gis_id.isdigit()): # FJK: regular exit from while-loop
735 break
736 else:
737 gis_id=gis_id.strip('abcdefghijklmnopqrstuvwxyz_') # FJK: to strip all letters
738 gis_id=gis_id.strip() # FJK: to strip all whitespaces
739 resultpoint = [0,0]
740 results = None
741 try:
742 if int(gis_id)>0:
743 SQL="SELECT x_coord,y_coord FROM chgis.chgis_coords WHERE gis_id LIKE cast("+ str(gis_id) +" as text);"
744 results=self.ZSQLSimpleSearch(SQL)
745 #print results
746 if results != None:
747 for result in results:
748 resultpoint=[getattr(result,str('x_coord')),getattr(result,str('y_coord'))]
749 if resultpoint !=[0,0]:
750 return resultpoint
751 else:
752 coords=self.getCoordsFromREST_gisID(gis_id)
753 SQL="INSERT INTO chgis.chgis_coords (chgis_coords_pkey,gis_id,x_coord,y_coord) VALUES (default," +gis_id+ "," +coords[0][1]+ "," +coords[0][0]+ "); ANALYZE chgis.chgis_coords;"
754 returnstring=self.ZSQLSimpleSearch(SQL)
755 return coords[0]
756 except:
757 return "gis_id not to interpretable:"+str(gis_id)
758 else:
759 return coords[0]
760
761 def getCoordsFromREST_gisID(self,gis_id):
762 gis_id=gis_id.strip()
763 coordlist=[]
764 i=0
765 while (i<5 and coordlist==[]):
766 urlinfo=urlFunctions.zUrlopenInfo(self,"http://chgis.hmdc.harvard.edu/xml/id/"+gis_id)
767 urlinfoLength=urlinfo.get('content-length')
768 if int(urlinfoLength)<500:
769 urlresponse=urlFunctions.zUrlopenRead(self,"http://chgis.hmdc.harvard.edu/xml/id/cts_"+gis_id)
770 else:
771 urlresponse=urlFunctions.zUrlopenRead(self,"http://chgis.hmdc.harvard.edu/xml/id/"+gis_id)
772 urlresponseString=urlFunctions.zUrlopenParseString(self,urlresponse)
773 baseDocElement= urlFunctions.zUrlopenDocumentElement(self,urlresponseString)
774 childnodes=urlFunctions.zUrlopenChildNodes(self,baseDocElement)
775 itemnodes=urlFunctions.zUrlopenGetElementsByTagName(self,baseDocElement,'item')
776 itemspatialnodes=None
777
778 for i in range(0,urlFunctions.zUrlopenLength(self,itemnodes)):
779 itemnode=urlFunctions.zUrlopenGetItem(self,itemnodes,i)
780 itemspatialnodes=urlFunctions.zUrlopenGetElementsByTagName(self,itemnode,'spatial')
781 if itemspatialnodes is not None:
782 for j in range(0,urlFunctions.zUrlopenLength(self,itemspatialnodes)):
783 coord=[]
784 itemspatialnode= urlFunctions.zUrlopenGetItem(self,itemspatialnodes,j)
785 itemspatiallatnodes=urlFunctions.zUrlopenGetElementsByTagName(self,itemspatialnode,'degrees_latitude')
786 for k in range(0,urlFunctions.zUrlopenLength(self,itemspatiallatnodes)):
787 itemspatiallatnode= urlFunctions.zUrlopenGetItem(self,itemspatiallatnodes,k)
788 coord.append(urlFunctions.zUrlopenGetTextData(self,itemspatiallatnode))
789 itemspatiallngnodes=urlFunctions.zUrlopenGetElementsByTagName(self,itemspatialnode,'degrees_longitude')
790 for k in range(0,urlFunctions.zUrlopenLength(self,itemspatiallngnodes)):
791 itemspatiallngnode= urlFunctions.zUrlopenGetItem(self,itemspatiallngnodes,k)
792 coord.append(urlFunctions.zUrlopenGetTextData(self,itemspatiallngnode))
793 coordlist.append(coord)
794 gis_id= "_"+gis_id
795 return coordlist
796
797 # End for GoogleMaps creation 614 # End for GoogleMaps creation
798 615
799 def RESTwrite2File(self,datadir, name,text): 616 def RESTwrite2File(self,datadir, name,text):
800 logging.debug("RESTwrite2File datadir=%s name=%s"%(datadir,name)) 617 logging.debug("RESTwrite2File datadir=%s name=%s"%(datadir,name))
801 try: 618 try: