kml2struct issue with loading from directory location
조회 수: 8 (최근 30일)
이전 댓글 표시
Hi peoples,
I am trying to read a .kml file using the kml2struct function stated below. I'm trying to call the function using a pathname to the file, code just below as well:
I keep getting an error saying index exceeds matrix dimensions but it works if i call the file directly without using fullfile. any suggestions why this is the case? (Mapping toolbox required for kml2struct) Thanks!
%Use function to get global variable for selected title
gettitle = getGlobalLoaded_Project_title;
%Put global loaded title string into text box
set(handles.map_loaded_project_title, 'String', gettitle);
%Search project directory for existing kml file within loaded project
map_file = dir(fullfile('Projects',gettitle,'\*.kml'));
map_file_array = struct2cell(map_file); %convert struct to array
if isempty(map_file_array) == 0 %check if array is not empty
%Load file path to text box
set(handles.txt_map_directory, 'String', fullfile('Projects',gettitle,map_file.name));
%run kml2struct
kml2struct(fullfile('Projects',gettitle,map_file.name))
end
%%%FUNCTION%%%
function kmlStruct = kml2struct(kmlFile)
[FID msg] = fopen(kmlFile,'rt');
if FID<0
error(msg)
end
txt = fread(FID,'uint8=>char')';
fclose(FID);
expr = '<Schema .+?>[\S\s]*?</Schema>';
schemaString = regexp(txt,expr,'match');
% grab each field def string
expr = '<SimpleField .+?>[\S\s]*?</SimpleField>';
schemaFields = regexp(schemaString,expr,'match');
% parse out just the field name
schemaFields = regexprep(schemaFields{1},'<SimpleField name="','');
schemaFields = regexprep(schemaFields,'".*','');
%%find the placemarks and put them in an array
expr = '<Placemark.+?>.+?</Placemark>';
objectStrings = regexp(txt,expr,'match');
Nos = length(objectStrings);
for ii = 1:Nos
% Find Object Name Field
bucket = regexp(objectStrings{ii},'<name.*?>.*?</name>','match');
if isempty(bucket)
name = 'undefined';
else
% Clip off flags
name = regexprep(bucket{1},'<name.*?>\s*','');
name = regexprep(name,'\s*</name>','');
end
kmlStruct(ii).Name = name;
% Find Object Description Field
bucket = regexp(objectStrings{ii},'<description.*?>.+?</description>','match');
if isempty(bucket)
desc = '';
else
% Clip off flags
desc = regexprep(bucket{1},'<description.*?>\s*','');
desc = regexprep(desc,'\s*</description>','');
end
kmlStruct(ii).Description = desc;
geom = 0;
% Identify Object Type
if ~isempty(regexp(objectStrings{ii},'<Point', 'once'))
geom = 1;
elseif ~isempty(regexp(objectStrings{ii},'<LineString', 'once'))
geom = 2;
elseif ~isempty(regexp(objectStrings{ii},'<Polygon', 'once'))
geom = 3;
end
switch geom
case 1
geometry = 'Point';
case 2
geometry = 'Line';
case 3
geometry = 'Polygon';
otherwise
geometry = '';
end
kmlStruct(ii).Geometry = geometry;
% Find Coordinate Field
bucket = regexp(objectStrings{ii},'<coordinates.*?>.*?</coordinates>','match');
% Clip off flags
if(~isempty(bucket))
coordStr = regexprep(bucket{1},'<coordinates.*?>(\s+)*','');
coordStr = regexprep(coordStr,'(\s+)*</coordinates>','');
% Split coordinate string by commas or white spaces, and convert string
% to doubles
coordMat = str2double(regexp(coordStr,'[,\s]+','split'));
% Rearrange coordinates to form an x-by-len matrix
[m,n] = size(coordMat);
coordMat = reshape(coordMat,2,m*n/2)';
% define polygon in clockwise direction, and terminate
[Lat, Lon] = poly2ccw(coordMat(:,2),coordMat(:,1));
if geom==3
Lon = [Lon;NaN];
Lat = [Lat;NaN];
end
end
kmlStruct(ii).Lon = Lon;
kmlStruct(ii).Lat = Lat;
kmlStruct(ii).BoundingBox = [[min(Lon) min(Lat);max(Lon) max(Lat)]];
% Schema Object Fields
for jj=1:length(schemaFields)
expr=strcat('<SimpleData.*?name="',schemaFields{jj},'".*?>.*?</SimpleData>');
bucket = regexp(objectStrings{ii},expr,'match');
if isempty(bucket)
name = 'undefined';
else
% Clip off flags
expr = strcat('<SimpleData .*?name="',schemaFields{jj},'".*?>\s*');
name = regexprep(bucket{1},expr,'');
name = regexprep(name,'\s*</SimpleData>','');
end
cmd = strcat('kmlStruct(ii).',schemaFields{jj} ,' = name;');
eval(cmd);
end
end
답변 (0개)
참고 항목
카테고리
Help Center 및 File Exchange에서 Language Support에 대해 자세히 알아보기
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!