一、环境、问题简述:

   1、采用的是前后端分离,后端需要将点位数据库点位的信息封装为geoJson数据类型,供前端利用echart 绘制地图;

   2、思路:简单理解geoJson,将对应坐标点位封装为geoJson格式返回到前端

  问题:

   1、封装点位,前端不能绘制出地图:第一给点位要求和最后一个点位必须相同,否则echart不能绘制出完整的封闭图形,从而绘制不出地图;

   2、绘制地图不全:POLYGON、MULTIPOLYGON 的数据封装类型格式是不一样的,后者按四维维数据封装,点位会绘制不出地图

  点位原数据格式(以字符串的形式存在数据库中):POLYGON((120.733031541 27.996004961,120.729992955 28.002353494,.....(具体数据见文末尾)

          MULTIPOLYGON(((120.69484819205 27.8010213272119,120.693914783313 27.8003668682123......(具体数据见文末尾)

在线绘图地址:http://geojson.io/#map=2/20.0/0.0

二、geoJson 数据格式简述

  1、数据格式:先了解geojson的格式类型:https://www.oschina.net/translate/geojson-spec

{ "type": "FeatureCollection",
"features": [
{ "type": "Feature",
"geometry": {"type": "Point", "coordinates": [102.0, 0.5]},
"properties": {"prop0": "value0"}
},
{ "type": "Feature",
"geometry": {
"type": "LineString",
"coordinates": [
[102.0, 0.0], [103.0, 1.0], [104.0, 0.0], [105.0, 1.0]
]
},
"properties": {
"prop0": "value0",
"prop1": 0.0
}
},
{ "type": "Feature",
"geometry": {
"type": "Polygon",
"coordinates": [
[ [100.0, 0.0], [101.0, 0.0], [101.0, 1.0],
[100.0, 1.0], [100.0, 0.0] ]
]
},
"properties": {
"prop0": "value0",
"prop1": {"this": "that"}
}
}
]
}
2、数据类型主要有Point(点)、MultiPoint(多点)、LineString(线)、MultiLineString(多线)、Polygon(面)、MultiPlygon(多面)、GeometryCollection(几何对象的集合),本次多
Polygon(面)、MultiPlygon(多面)为转换点位;

3、对象类创建
  1)、点位类集合(包含所有的点位)

    

public class PostionList {
    private String type = "FeatureCollection";     private List<SinglePosition> features = new ArrayList<>();                               }

  2)、单个点位对象

  

 public class SinglePosition {

      private String type = "Feature";

      private Properties properties;

      private Geometry geometry;

    }

  3)、点位属性实体类 (单个点位的名称、数据等放在这里)

    public class Properties {
      private String name;       private Long orgId;     }
  4)、单个坐标实体类数组

  

  public class Geometry {
      private String type;       private Object coordinates ; //可能为三维数组或者四维数组     }

  总体类结构图

MULTIPOLYGON、POLYGON   封装为echart  geoJson  数据封装-LMLPHP

4、点位转换工具类,将数据库取出的字符串,转换为对应坐标类型

  

public PostionList getSinglePositionByOrgId(Long orgId) {
    PostionList postionList = new PostionList();
    List<SinglePositonDomain> list = singlePositionMapper.getSinglePositionByParentOrgId(orgId);
    List<SinglePosition> features = new ArrayList<>();
    if(list!=null && list.size()>0) {
      for(SinglePositonDomain singlePosition: list) {
        SinglePosition position = new SinglePosition();
        Properties properties = new Properties();
        properties.setName(singlePosition.getOrgName());
        properties.setOrgId(singlePosition.getOrgId());
        Geometry geometry = new Geometry();
        if(singlePosition.getPositionString().startsWith("POLYGON")) {
          //三维数组
          geometry.setType("Polygon");
          List<Double [][]> coordinates =new ArrayList<Double [][]>();
          String points = singlePosition.getPositionString();
          points = points.substring(points.indexOf("(")+2, points.indexOf(")"));
        if(points.contains("(")) {
          points = points.substring(points.indexOf("(")+1, points.length());
        }
          Double [][] pointarr = dealString(points);
          coordinates.add(pointarr);
          geometry.setCoordinates(coordinates);
          }else if(singlePosition.getPositionString().contains("MULTIPOLYGON")) {
            geometry.setType("MultiPolygon");
            //四维数组
            String points = singlePosition.getPositionString();
            points = points.substring(points.indexOf("(")+3, points.lastIndexOf(")")-2);             points = points.replace(")),((",";");
            String[] arrs2 = points.split(";");             List<List<Double [][]>> coordinates =new ArrayList<List<Double [][]>>();
            List<Double [][]> array = new ArrayList<Double [][]>();
            for(String str: arrs2) {
              Double [][] pointarr = dealString(str);
              array.add(pointarr);
            }
            coordinates.add(array);
            geometry.setCoordinates(coordinates);
            }
            position.setGeometry(geometry);
            position.setProperties(properties);
            features.add(position);
          }
          }
          postionList.setFeatures(features);           return postionList;
         }     private Double [][] dealString(String points) {
      String[] arrs = points.split(",");
      List<String[]> result = new ArrayList<String[]>();
      Double [][] pps= new Double[arrs.length][2];
      Double [][] newPps= new Double[arrs.length+1][2];
      for (int i = 0; i < arrs.length; i++) {
      result.add(arrs[i].split(" "));
      String str = arrs[i];
      str = str.trim();
      String[] ps = str.split(" ");
      if(ps.length ==2) {
        pps[i][0] = Double.valueOf(ps[0]);
        pps[i][1] = Double.valueOf(ps[1]);
        newPps[i][0] = Double.valueOf(ps[0]);
        newPps[i][1] = Double.valueOf(ps[1]);
      }else {
      continue;
      }
      //绘图要求第一个和最后一个坐标相同,如果不同,将第一个作为最后一个
      if((i==arrs.length-1)&&pps[0][0]!=pps[arrs.length-1][0]&&pps[0][1]!=pps[arrs.length-1][1]) {
        newPps[arrs.length][0]=pps[0][0];
        newPps[arrs.length][1]=pps[0][1];
        return newPps;
      }
     }
  return pps;
}

四、点位类型提供:

geoJson格式文档测试:https://files.cnblogs.com/files/liweiweicode/map.zip   可直接放入测试网站测试;

MULTIPOLYGON、POLYGON   封装为echart  geoJson  数据封装-LMLPHP

类型1:POLYGON((120.733031541 27.996004961,120.729992955 28.002353494,120.725639213 27.995536313,120.721104803 27.994265268,120.709499662 27.996739926,120.705072839 27.986666666,120.719248868 27.983292379,120.725205102 27.967698525,120.712518138 27.949266348,120.720730507 27.94833389,120.723801384 27.940917731,120.727052138 27.942719961,120.726180448 27.940862179,120.736767479 27.93838951,120.742208521 27.941153677,120.741178129 27.937216372,120.745111756 27.935453439,120.74135271 27.928899364,120.745445912 27.929732278,120.74530466 27.927045858,120.741159456 27.923045398,120.737113674 27.924539454,120.738077559 27.918137241,120.742002961 27.917801066,120.738649025 27.912775482,120.746476459 27.906735279,120.745066684 27.901358911,120.748287475 27.896746836,120.745117464 27.889560456,120.740234086 27.8878814,120.750084726 27.886264848,120.751533678 27.879267321,120.74370648 27.880264914,120.74097391 27.876847927,120.727925329 27.886399174,120.736785786 27.886601545,120.720669262 27.886781271,120.72774933 27.885661676,120.737509049 27.878497158,120.738586601 27.87617553,120.734704411 27.877347474,120.732398349 27.873680312,120.747214859 27.8669238,120.751299942229 27.8723555894651,120.757136429046 27.8759604783811,120.762629593108 27.8809386583127,120.76589115927 27.879908690051,120.770869339202 27.8817969651975,120.779624069426 27.8783637376584,120.785117233489 27.8795653672971,120.789065445159 27.8781920762815,120.790953720305 27.8790503831662,120.789408767913 27.8807669969358,120.796446884368 27.8848868699826,120.795931900237 27.8867751451291,120.797133529876 27.8898650499143,120.799880111907 27.8922683091916,120.803485000823 27.8922683091916,120.803485000823 27.8948432298459,120.806746566985 27.8979331346311,120.810694778655 27.8951865525998,120.81550129721 27.8888350816526,120.818419540618 27.8857451768674,120.822024429534 27.8783637376584,120.825285995696 27.8730422349729,120.828890884612 27.868750700549,120.830264175628 27.8611975999631,120.83180912802 27.8562194200315,120.839018905852 27.8436881395139,120.87160730923434 27.87220299247342,120.87198033185317 27.872549795707528,120.874222155 27.893249771,120.87747710189976 27.896111907471493,120.882225250896 27.9165098558461,120.88230571961826 27.916517204064007,120.882439565 27.917354642,120.896596409116 27.9304448761218,120.896296001707 27.9302410282367,120.893892742429 27.9302410282367,120.886983372007 27.9373113312,120.867757297788 27.9469243683093,120.854024387632 27.9585973419421,120.842737652097       27.9710964359516,120.840291477476 27.973703543114,120.839604831968 27.9750768341296,120.83861777905 27.9734996952289,120.840139128647 27.9747603432312,120.829388416 27.978032985,120.801872235 27.984206352,120.789253787     27.977796049,120.776929944 27.977220075,120.764001335 27.980737858,120.745596079 27.993326569,120.742579303 27.989279306,120.733031541 27.996004961));

  类型二:  MULTIPOLYGON(((120.69484819205 27.8010213272119,120.693914783313 27.8003668682123,120.69332469733 27.7992725269342,120.695438278033 27.798156727984,120.697584045245 27.8003024951959,120.69484819205 27.8010213272119)),((120.344448395 27.991393955,120.34604707882828 27.98745719307442,120.346404642361 27.9872565921189,120.34622818159774 27.987011227164942,120.346274513 27.986897136,120.34611107927365 27.986848398863533,120.34274833386226 27.98217257259043,120.33808045668827 27.975681988243537,120.337989745 27.974947318,120.3341616188371 27.975802771423712,120.33408019703519 27.975813638593518,120.33390765196513 27.975836667764234,120.325154192 27.970837581,120.32407332453411 27.977149231520343,120.30701104777724 27.979426492087782,120.306818961 27.979375938,120.287928719 27.981674893,120.284955086 27.973961478,120.28129748504294 27.97235458030815,120.27252400368276 27.959463160415424,120.272141203455 27.958900688317,120.2709349453871 27.95942435164561,120.26842938815072 27.960512069504112,120.258859844 27.959781313,120.252995046 27.964940834,120.2535190441456 27.96698497988723,120.25337708921622 27.96704660566435,120.24751564699696 27.969591187478727,120.24739090854389 27.969250442646455,120.244910493625 27.9470921887544,120.23819666387503 27.942539636299557,120.235729976 27.936156254,120.230588795 27.93690364,120.224953755 27.933021147,120.223164592 27.928812738,120.226551792 27.920847838,120.22040937 27.918785677,120.216739023 27.909959212,120.225120024 27.897703399,120.223830679 27.889217599,120.215416643 27.890141393,120.211821417 27.885814535,120.205433578 27.884768964,120.205475596 27.875855004,120.196508258 27.875884364,120.19226119 27.869990564,120.184342788 27.869835638,120.195078938 27.85469455,120.192459881 27.85236536,120.193662654 27.83393461,120.186808103 27.829315128,120.186544875 27.825883837,120.180269441 27.824815892,120.180055008 27.814471294,120.171652347 27.810390373,120.169177759 27.804683648,120.19697017 27.792737592,120.197680476 27.788375007,120.19023899 27.782594139,120.202310229 27.783007773,120.20969545 27.779938696,120.209330504 27.775890008,120.215448691 27.772443255,120.21094631 27.766248057,120.227107236 27.759386426,120.239733563 27.747591599,120.251208729 27.743900333,120.253168244 27.738272927,120.250166069 27.739338928,120.247045642 27.73602042,120.247794855 27.726111972,120.251062385 27.724387088,120.255252049 27.723718928,120.258879771 27.728508469,120.267006404 27.726992823,120.267167582 27.730866161,120.279702674 27.735289752,120.276123834 27.737448542,120.278504656 27.740192645,120.283078684 27.736625291,120.284386759 27.73926502,120.305088829 27.720099589,120.310242204 27.726150435,120.3055969 27.729214131,120.307337884 27.734380048,120.303390717 27.738231586,120.310436456 27.737364439,120.318902499 27.741308602,120.321394935 27.745329598,120.319803285 27.751610751,120.322897975 27.753297816,120.331253658 27.742930053,120.338914179 27.739896649,120.341089407 27.732132682,120.349681194 27.73268669,120.354637073 27.727431532,120.365246806 27.729702211,120.381528893 27.721885854,120.385505388 27.716457047,120.38028548 27.709264285,120.389344331 27.704939957,120.387324552 27.700036112,120.395388059 27.695177162,120.398584027 27.685837536,120.405962213 27.682204361,120.408777247 27.684063078,120.402844768 27.68818341,120.404526208 27.691123414,120.416304093 27.688714173,120.416888645 27.696598095,120.425187861 27.690503961,120.425577167 27.693078577,120.434941772 27.690758021,120.445571535 27.680924505,120.445155664 27.6758249,120.455061738 27.668943716,120.472028814 27.674810655,120.471439743 27.678738035,120.479197016 27.681573449,120.482571145 27.689114226,120.479615953 27.690892851,120.484646305 27.692536438,120.486539725 27.698103567,120.483863643 27.701329894,120.486071589 27.704829807,120.48372765 27.712421507,120.487028628 27.719649605,120.498000447 27.715671744,120.500414295 27.721787588,120.510949426 27.724373716,120.512011261 27.73196135,120.517483636 27.73662813,120.515487797 27.740464044,120.524913002 27.752592714,120.546635789 27.765721222,120.555816399 27.764237828,120.57100175 27.746168826,120.584598287 27.758878137,120.596946311 27.746397721,120.599186601 27.747334656,120.614396021 27.725621047,120.604487697 27.711326344,120.600879334 27.711242925,120.603148073 27.708840225,120.600134775 27.705871229,120.601882383 27.698614912,120.610986912 27.6933822,120.615025747 27.697589738,120.628288042 27.692587432,120.6267399 27.700521242,120.635007514 27.697580227,120.646239755 27.698366908,120.648167169 27.694386927,120.68878029362489 27.678692159458357,120.69178395017 27.6880312201656,120.679935608842 27.7004828366145,120.70315714099 27.7164158663001,120.72019502738 27.7077538355719,120.830428493335 27.6516544715906,121.048781764819 27.5362980262781,121.200530422046 27.4985325233484,121.22536975118304 27.505487535506912,121.245998951748 27.5261167360718,121.2692878423999 27.56858471314223,121.2693549722619 27.568760529447445,121.27623477073062 27.617779093537138,121.27805644745794 27.647836759536858,121.26171836672937 27.654088308111575,120.764411696 27.811148423,120.759451082 27.812085827,120.744030258 27.827717187,120.74432646 27.84033286,120.754154979 27.852403588,120.748375662 27.865530334,120.739687831 27.87137082,120.711227435 27.873041402,120.716630337 27.877743834,120.723190877 27.877496488,120.714832016 27.878864377,120.710549164 27.874966429,120.705276543 27.877387297,120.696568601 27.874384713,120.694478369 27.869209134,120.680275935 27.868390889,120.683764562 27.862296695,120.678521894 27.863152837,120.676751619 27.855476586,120.667766975 27.853265989,120.66398656 27.85684931,120.647542478 27.85145992,120.631645965 27.86252648,120.610961787 27.853774336,120.611169016 27.86081316,120.618016858 27.867531651,120.618750671 27.874863203,120.611353698 27.882463236,120.606651638 27.882285496,120.608768808 27.889327817,120.612866703 27.891116443,120.597603677 27.898375139,120.592377653 27.906588195,120.594502222 27.909921538,120.586803312 27.902001612,120.574595353 27.902724317,120.574354429 27.898120166,120.572615012 27.903315797,120.56486552 27.906357816,120.560598106 27.901312002,120.556451736 27.902181857,120.558458672 27.900723902,120.550205772 27.894129163,120.547407206 27.899425594,120.53069724 27.904019139,120.538441343 27.908429972,120.536565719 27.910936716,120.523588005 27.916418694,120.510422425 27.915177025,120.510401087 27.926962375,120.500636871 27.932968925,120.482365342 27.933283014,120.480205 27.936056641,120.494684824 27.941326414,120.494787492 27.944790665,120.499614844 27.94718749,120.494659342 27.952281814,120.498354752 27.95689636,120.49019144 27.958858499,120.492642642 27.966902902,120.483684464 27.966659144,120.488051841 27.970370365,120.487284428 27.97381947,120.481480011 27.96837523,120.463221944 27.968688229,120.461275218     27.973243923,120.452950093 27.974631117,120.449001088 27.979559149,120.446449688 27.974146225,120.434679503 27.975285948,120.42893209 27.966633857,120.426216852 27.97009159,120.411075778 27.969611737,120.407263504     27.965913135,120.410475409 27.95913671,120.385600285 27.980657895,120.374984166 27.985694829,120.368014522 27.984234814,120.349083502 27.988421062,120.344448395 27.991393955)));

邮箱:[email protected]   博客地址:https://www.cnblogs.com/liweiweicode

05-28 19:28