SELECT Id, Name, FSL__Description__c, FSL__Color__c, FSL__KML__c, FSL__Service_Territory__c, FSL__Service_Territory__r.FSL__TerritoryLevel__c, FSL__Mi_La__c, FSL__Ma_La__c, FSL__Mi_Lo__c, FSL__Ma_Lo__c FROM FSL__Polygon__c WHERE FSL__Service_Territory__c != null AND FSL__Mi_La__c <= :lat AND FSL__Ma_La__c >= :lat AND FSL__Mi_Lo__c <= :lon AND FSL__Ma_Lo__c >= :lon AND FSL__Service_Territory__r.RecordType.DeveloperName =:requestType LIMIT 10000
For the result I will get, I can extract the KML file, that look like
SELECT Id, Name, FSL__Description__c, FSL__Color__c, FSL__KML__c, FSL__Service_Territory__c, FSL__Service_Territory__r.FSL__TerritoryLevel__c,
FSL__Mi_La__c, FSL__Ma_La__c, FSL__Mi_Lo__c, FSL__Ma_Lo__c FROM FSL__Polygon__c
WHERE FSL__Service_Territory__c != <kmlnull xmlns="http://www.opengis.net/kml/2.2">
AND FSL__Mi_La__c <= :lat
AND FSL__Ma_La__c >= :lat
AND FSL__Mi_Lo__c <= :lon
AND FSL__Ma_Lo__c >= :lon
<StyleAND id="GreatFSL__Service_Territory__r.RecordType.DeveloperName Toronto=:requestType CoreStyle">LIMIT 10000
For the result I will get, I can extract the KML file, that look like
<?xml version="1.0" encoding="UTF-8"?>
<kml xmlns="http://www.opengis.net/kml/2.2">
<Style id="Great Toronto CoreStyle">
<LineStyle>
<width>1</width>
</LineStyle>
<PolyStyle>
<color>80E5881E</color>
</PolyStyle>
</Style>
<Placemark>
<name>Great Toronto</name>
<styleUrl>#Great TorontoStyle</styleUrl>
<Polygon>
<outerBoundaryIs>
<LinearRing>
<coordinates>-79.33801613769532,43.767698789150835,0
-79.33589182815554,43.75804421098782,0
-79.33401139070469,43.75160149776085,0
-79.33366517681036,43.74814593102189,0
-79.3325336326381,43.7426243489531,0
-79.33226037535184,43.73924674477029,0
-79.32456269368157,43.7408808698677,0
...
-79.33589182815554,43.75804421098782,0 -79.33401139070469,43.75160149776085,0 -79.33366517681036,43.74814593102189,0 -79.3325336326381,43.7426243489531,0 -79.33226037535184,43.73924674477029,0 -79.32456269368157,43.7408808698677,0 ...